CSS壓縮公用程式(自動合并重複的定義)

來源:互聯網
上載者:User

最近一個項目,css主檔案居然到了9800行,我震驚了。

於是寫了個合并相同css的php。

 

合并之後有點點小問題,我難得去找什麼問題了,稍微改下css就ok。有個壓縮功能,預設開啟的,不過沒試。

 

<?phpdefine('CMP', $argv>1?$argv[1]:true);//壓縮if(CMP){define('SPA','');define('TAB','');define('NL','');}else{define('SPA',' ');define('NL',"\n");}define('TAB',"\t");$str = file_get_contents('green1.css');$str = preg_replace('/\/\*.*\*\//', '',$str);$str = preg_replace('/\/\/.*/', '',$str);/*echo '=========================='.$str.'==========================';*/preg_match_all("/([^{]+)\{([^}]+)}/", $str, $out);$arr = array();echo 'There are :'.count($out[0]).' css classes'.chr(10);for($i=0;$i<count($out[1]);$i++){$out[1][$i] = trim($out[1][$i]);$left = substr($out[1][$i],0,1);if($left=='.'){$left = 'dot';$right = substr($out[1][$i],1);}else if($left=='#'){$left = 'jin';$right = substr($out[1][$i],1);}else {$left = 'emp';$right = $out[1][$i];}$cls = $left.$right;if(isset($arr[$cls])) $arr[$cls] = array();$stys = explode(';', $out[2][$i]);for($j=0;$j<count($stys);$j++){$tmp = explode(':', $stys[$j]);if(count($tmp)==2){$arr[$cls][trim($tmp[0])] = trim($tmp[1]);}else if(strlen(trim($stys[$j]))>0){echo 'Error: '.$stys[$j].chr(10);}}}$str = '';foreach($arr as $cls=>$v){$pre = substr($cls,0,3);$left = substr($cls,3);if($pre=='dot'){$cls = '.'.$left;}else if($pre=='jin'){$cls = '#'.$left;}else if($pre=='emp'){$cls = $left;}file_put_contents('green.css', $cls.'{'.NL, FILE_APPEND);foreach ($v as $sty=>$txt)file_put_contents('green.css', TAB.$sty.':'.SPA.$txt.";".NL, FILE_APPEND);file_put_contents('green.css', '}'."\r\n", FILE_APPEND);}

 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.