Last Updated: February 25, 2016
·
1.969K
· 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);

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside