Minify CSS using preg_replace

Posted by x3ro on Stack Overflow See other posts from Stack Overflow or by x3ro
Published on 2009-09-04T13:30:19Z Indexed on 2010/05/13 3:54 UTC
Read the original article Hit count: 245

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about regex