Last Updated: February 25, 2016
·
1.436K
· barnettjw

Remove whitespace from CSS using PHP

Here's a great PHP script to minify CSS on-the-fly. However it removes all whitespace, that's a problem because it breaks some CSS.

So I fixed it...

This will strip whitespace from CSS, but leave syntactically significant whitespace for descendant selectors & shorthand properties.

$buffer = str_replace(array("\r\n", "\r", "\n", "\t"), '', $buffer);
$buffer = ereg_replace(" {2,}", ' ',$buffer);