CSS compression tool (automatically merge repeated definitions)

Source: Internet
Author: User

In a recent project, the main CSS file actually reached 9800 lines, and I was shocked.

So I wrote a PHP code that combines the same CSS.

 

After merging, there are a few small issues. I have a hard time looking for any issues. Just change the CSS to OK. There is a compression function, which is enabled by default, but I did not try it.

 

<? Phpdefine ('cmp ', $ argv> 1? $ Argv [1]: True); // compress if (CMP) {define ('pa', ''); define ('tab ',''); define ('nl ', '');} else {define ('pa',''); 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 classe '. 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 ); $ left = substr ($ CLs, 3); if ($ pre = 'dot ') {$ CLS = '. '. $ left;} else if ($ pre = 'jin') {$ CLS = '#'. $ left;} else if ($ pre = 'emp') {$ CLS = leleft?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_append1_1_file_put_contents('green.css ','}'. "\ r \ n", file_append );}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.