Minify CSS using preg_replace

此生再无相见时 提交于 2020-01-01 10:48:36

问题


I'm trying to minify multiple CSS files using preg_replace. Actually, I'm only trying to remove any linebreaks/tabs and comments from the file. the following works for me:

$regex = array('{\t|\r|\n}', '{(/\*(.*?)\*/)}');
echo preg_replace($regex, '', file_get_contents($file));

But I'd like to do it in a single multiline regex, like this:

$regex = <<<EOF
{(
    \t
|
    \r
|
    \n
|
    /\*(.*?)\*/
)}x
EOF;
echo preg_replace($regex, '', file_get_contents($file));

However, this does not do anything at all. Is there any way to do this?


Edit: Ok, so I'll take a look at existing minifiers, but it still leaves me with the question how I would do a multiline regex like this, because with the x-modifier multiline regexs should work fine even in php, shouldn't they?


回答1:


There are utilities available that may fit your needs and save you a potentially buggy regex.

The YUI compressor supports minification of CSS and javascript files.

You may wish to consider this or other existing options before writing your own.




回答2:


I'm not sure how you would do that, but here is a script my friend wrote that is pretty fast at minifying CSS:

function minimize_css($input)
{
    // Remove comments
    $output = preg_replace('#/\*.*?\*/#s', '', $input);
    // Remove whitespace
    $output = preg_replace('/\s*([{}|:;,])\s+/', '$1', $output);
    // Remove trailing whitespace at the start
    $output = preg_replace('/\s\s+(.*)/', '$1', $output);
    // Remove unnecesairy ;'s
    $output = str_replace(';}', '}', $output);
    return $output;
}



回答3:


This is what I personally use for CSS:

$file_contents = file_get_contents($file);<br />
preg_replace('@({)\s+|(\;)\s+|/\*.+?\*\/|\R@is', '$1$2 ', $file_contents);



回答4:


This seems like a perfect example of when not to re-invent the wheel. Nearly every site on the internet uses CSS, and all the big ones are compressing it somehow. Their methods are already tested and optimized. Why roll your own if you don't have to?

Mike and Grumbo have given specific suggestions already, but I just wanted to point out the general principle.




回答5:


As far as I know you can't do that because you're actually changing the pattern when you break it into multiple lines.

Edit: and yeah, +1 for not re-inventing the wheel.




回答6:


This is what i am using in the Samstyle PHP Framework:

$regex = array(
"`^([\t\s]+)`ism"=>'',
"`([:;}{]{1})([\t\s]+)(\S)`ism"=>'$1$3',
"`(\S)([\t\s]+)([:;}{]{1})`ism"=>'$1$3',
"`\/\*(.+?)\*\/`ism"=>"",
"`([\n|\A|;]+)\s//(.+?)[\n\r]`ism"=>"$1\n",
"`(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+`ism"=>"\n"
);
$buffer = preg_replace(array_keys($regex),$regex,$buffer);

Hope this helps!




回答7:


function minifyCSS($css){
    $css = trim($css);
    $css = str_replace("\r\n", "\n", $css);
    $search = array("/\/\*[^!][\d\D]*?\*\/|\t+/","/\s+/", "/\}\s+/");
    $replace = array(null," ", "}\n");
    $css = preg_replace($search, $replace, $css);
    $search = array("/;[\s+]/","/[\s+];/","/\s+\{\\s+/", "/\\:\s+\\#/", "/,\s+/i", "/\\:\s+\\\'/i","/\\:\s+([0-9]+|[A-F]+)/i","/\{\\s+/","/;}/");
    $replace = array(";",";","{", ":#", ",", ":\'", ":$1","{","}");
    $css = preg_replace($search, $replace, $css);
    $css = str_replace("\n", null, $css);
    return $css;    

}


http://mhameen.blogspot.com/2010/04/crystal-script-manger-for-php.html#links



来源:https://stackoverflow.com/questions/1379277/minify-css-using-preg-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!