Compressing CSS Files on the Fly

Compression can be done using Apache or PHP settings. If you are not using those two methods and using this method of clearing cached file on Client side then the code below by Reinhold Weber can be useful.

Using the below method for trimming whitespace and comments may not be very good if you are not using some kind of source caching. Without source caching, this code need to run every time so it should be better to enable the compression using Apache or PHP setting!

<?php
function compressCSS($buffer) {
	/* remove comments */
	$buffer = preg_replace('!/*[^*]**+([^/][^*]**+)*/!', '', $buffer);
	/* remove tabs, spaces, newlines, etc. */
	$buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
	return $buffer;
}
?>

Apply the above compress() code to the content of the new file and it will save some bandwidth on server for you and give better browsing experience to user.

  • # 1 - by web tasarım seo

    awesome post thank you ..I can found very good articles and subjects here bravo..

Comments are open for an year period. Please, write here on Facebook page.