CSS Disassembler

  1. <?php
  2.  
  3. function dissasemble_css($css) {
  4.   // Formatted statements.
  5.   $formatted_statements = array();
  6.   // Remove comments.
  7.   $css = preg_replace("/\/\*(.*)?\*\//Usi", "", $css);
  8.   // Split out each statement
  9.   $statements = explode("}", $css);
  10.   // If we have any statements, parse them.
  11.   if (count($statements) > 0) {
  12.     // Iterate through all of the statements.
  13.     foreach ($statements as $statement) {
  14.       $formatted_statement = array('keys' => array(), 'value' => array());
  15.       // Get the key and the code for the key.
  16.       list($key, $code) = explode("{", $statement);
  17.       // Get all keys individually.
  18.       $keys = explode(",", trim($keystr));
  19.       // If we have more than one key, handle them.
  20.       if (count($keys) > 0) {
  21.         // Iterate through all the keys and add them to the array.
  22.         foreach ($keys as $key) {
  23.           // Replace un-needed characters and do a little cleanup.
  24.           $key = preg_replace("/[\n|\t|\\|\s]+/", ' ', strtolower(trim($key)));
  25.           // Make sure this is a real key.
  26.           if (strlen($key) > 0) {
  27.             // Add the key to the array.
  28.             $formatted_statement['keys'][] = $key;
  29.           }
  30.         }
  31.       }
  32.       // It's easier to handle and looks nicer if it's lower case.
  33.       $code = strtolower($code);
  34.       // Make sure the explosion is big!
  35.       $code_fragements = explode(";", $code);
  36.       // Make sure there actually are any remains from the explosion.
  37.       if (count($code_fragements) > 0) {
  38.         // Iterate through the remains and parse them.
  39.         foreach ($codes as $code) {
  40.           // Explode the remaining fragements some more, but clean them up first.
  41.           $code_remains = explode(':', preg_replace("/[\n|\t|\\|\s]+/", ' ', strtolower(trim($code))));
  42.           // If the key (the first part) actually exists, then add it.
  43.           if(strlen($code_remains[0]) > 0) {
  44.             // Add it to the array
  45.             $formatted_statement['value'][trim(array_shift($code_remains))] = trim(implode(':', $code_remains));
  46.           }
  47.         }
  48.       }
  49.     }
  50.   }
  51.   // Return the statements.
  52.   return $formatted_statements;
  53. }