- Jul. 21st, 2006
- 2 comments
This function, comprised of a simple regular expression, will remove most of the bloat from larger CSS files. Not that the effects are very substatial or groundbreaking, but it does save quite a few kilobytes to run things like this over your CSS and HTML. Using mod_gzip also has a favorable effect, but this cannot hurt either. If you find any real bugs, let me know; if it's something completely pathological and contrived, don't let me know. I have another filter for HTML; but I'll post it another day. Here it is:
<?
function trimCSS($str)
{
return preg_replace('#(?<=[:;\w{])\s+(?=[}\w;:\#])#', '', $str);
}
?>
Related posts:
"2 Wise Comments Banged Out Somewhere On The Internet ..."
Do you place this in your css file? And then turn on php processing for css files via htaccess? I've done that, but didn't really notice a difference - How can I tell if its working? And any chance of getting the same snippet for the html files? I'm big on reducing code bloat
This regex didn't work for me for some reason. I made an alternate set that does… and also strips comments. The only thing you really need whitespace-wise is a single space after '}' and a space between CSS properties ("1px solid #000″). Everything else is bloat.
|
















