PHP 操作 CSS

 

<?
/*******************************
PHP 操作 CSS ------- hzqghost from Yiparts.com
******************************
*/
class YPCSS{
    
var $cssFileName;
    
var $body;
    
var $CSSARR;
    
function YPCSS($filename){
        
$this->cssFileName=$filename;
        
$this->Body=file_get_contents($this->cssFileName);
        
$this->Body=preg_replace("/(\w)\{/", "$1 {", $this->Body);
        
$this->Body=preg_replace("/\}(\w)/", "} $1", $this->Body);
        
$pattern = "/[^\{\}]+\{([^\{\}]|\{?R\})*\}/is";
        
preg_match_all($pattern,$this->Body,$match);
        
foreach($match[0] as $k=>$v){
            
$pattern = "/([^\{\}]+)\{(.*)\}/is"; 
            
preg_match($pattern,$v,$match1);    
            
$pattern = "/([^:;]+):([^:;]+|\{?R\})*;/is"; 
            
preg_match_all($pattern,$match1[2],$match2);
            
foreach($match2[1] as $k2=>$v2){    
                
$this->CSSARR[trim($match1[1])][trim($v2)]=trim($match2[2][$k2]);
            }
        }    
    }
    
function ToCss(){
        
$data='';
        
foreach($this->CSSARR as $k=>$v){
            
$items='';
            
foreach($v as $ik=>$iv){
                
$items.=$ik.':'.$iv.';';
            }
            
$data.=$k.'{'.$items.'}'."\r\n";
        }
        
file_put_contents($this->cssFileName,$data);    
    }
}
$css = new YPCSS('main.css');
$css->CSSARR['#lang']['FLOAT']='left';
$css->ToCss();
?>

 

posted @ 2009-04-13 13:52  幽灵湖  阅读(262)  评论(0)    收藏  举报