Minify CSS using preg_replace

2020-07-13 08:13发布

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?

7条回答
【Aperson】
2楼-- · 2020-07-13 09:00

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.

查看更多
Deceive 欺骗
3楼-- · 2020-07-13 09:03

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.

查看更多
倾城 Initia
4楼-- · 2020-07-13 09:03

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;
}
查看更多
倾城 Initia
5楼-- · 2020-07-13 09:05
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

查看更多
混吃等死
6楼-- · 2020-07-13 09:09

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);
查看更多
Emotional °昔
7楼-- · 2020-07-13 09:17

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.

查看更多
登录 后发表回答