5

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 use a multiline regex like this? With the x flag, multiline expressions should work fine in PHP.

2
  • 2
    Better use a compression like deflate or gzip. That will give you better results. Commented Sep 4, 2009 at 13:53
  • An example regex CSS minifier can be found here. Commented Mar 4, 2013 at 6:41

4 Answers 4

11

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;
}
Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

2

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);

1 Comment

Please explain which character in your pattern the regex engine is suppose to treat case-insensitively. There are two instances of unnecessary escaping in your pattern. Of course preg_replace() doesn't modify by reference, so it is misleading that the returned value is never used.
0
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

1 Comment

Your link is dead. Was this page where you found this code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.