Minify CSS using preg_replace

php, regex

Solution

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.

Problem

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.

Original source

Related problems