CSS Compression Function

Nice function, I use it in pretty much every application I develop. I can’t guarantee that it’s safe but in all the time I’ve used it I’ve never had a problem.

I went on one of those CSS compression sites, and this function that I had wrote reduced my CSS file to 2506 bytes, exactly the same size as the most advanced compression the site offered.

function compressCss(scalar $string) {
$blank = array(”\r”, “\n”, “\t”);
$string = str_replace($blank, ”, $string);
$string = str_replace(’ {’, ‘{’, $string);
$string = str_replace(’;', ‘;’, $string);
$string = str_replace(’: ‘, ‘:’, $string);
$string = str_replace(’, ‘, ‘,’, $string);
$string = str_replace(’;}’, ‘}’, $string);
$string = str_replace(’ }’, ‘}’, $string);
$string = preg_replace(
array(’#/\*.*?\*/#s’), ”, $string
);
return trim($string);
}

Leave a Reply