Advertisement
omnosis

CSS parser

May 22nd, 2011
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 KB | None | 0 0
  1. <?php
  2.     header("Content-Type: text/css; charset=UTF-8");
  3.  
  4.    
  5.     function parse_css($name) {
  6.         $css = "";
  7.         $name_arr = array();
  8.         $value_arr = array();
  9.         $file;
  10.        
  11.         $file=fopen("$name","r") or exit("");
  12.         $constants = array();
  13.         while (!feof($file)) {$css .= fgetc($file);}
  14.         fclose($file);
  15.    
  16.         //search all declaration
  17.         preg_match_all("/@(\w+).*:[^;]+;/",$css,$constants);
  18.        
  19.         for($i=0;$i<sizeof($constants[1]);$i++) {//parse declarations
  20.             $splitted = preg_split("/@(\w+).*:(\s)*/",$constants[0][$i]);
  21.             $splitted = preg_split("/;/",$splitted[1]);
  22.             $value_arr[$i] = $splitted[0];
  23.             $name_arr[$i] = $constants[1][$i];         
  24.             $splitted = "";        
  25.         }
  26.  
  27.         for($i=0;$i<sizeof($name_arr);$i++) {//remove declarations
  28.             $css=preg_replace('/@'.$name_arr[$i].'(\s)*(:)(\s)*'.$value_arr[$i].'(\s)*(;)/','',$css);
  29.         }
  30.        
  31.         for($i=0;$i<sizeof($name_arr);$i++) {//replace
  32.             $css=preg_replace('/@'.$name_arr[$i].'/',$value_arr[$i],$css);
  33.         }
  34.         echo $css;
  35.     }
  36.        
  37.     $files = array_keys($_GET);
  38.     foreach($files as $file) {
  39.         if(preg_match("/(((\\/)?[a-zA-Z0-9_]+)+)(_css)$/",$file)==1) {
  40.             $file=preg_replace("/(_css)/",".css",$file);
  41.             if(file_exists($file)) {
  42.                 parse_css($file);
  43.             }
  44.         }
  45.     }
  46.  
  47.    
  48.  
  49.  
  50. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement