views:

458

answers:

6

Hey folks,

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?

Regards, x3ro

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?

A: 

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.

Vinicius Pinto
+4  A: 

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.

Mike
+1  A: 

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;
}
Eddie Ringle
+1  A: 

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.

Nathan Long
A: 

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!

thephpdeveloper
A: 
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

m. h. ameen