Efficient techniques to compress css files in php

Efficient techniques to compress css files in php

Today I’m going to share 3 most efficient techniques to compress css files using php. All of them are working great for any application. You know, for a complex design we need to write a lot’s of css script and css files need to load fast for displaying website interface properly. Big css files take more time to load, but we can easily solve this using php. So, lets see compressing techniques –

Technique 01:

ob_start ("ob_gzhandler");
header ("content-type: text/css; charset: UTF-8");
header ("cache-control: must-revalidate");
$offset = 60 * 60;
$expire = "expires: " . gmdate ("D, d M Y H:i:s", time() + $offset) . " GMT";
header ($expire);

Technique 02:

This technique remove all unnecessary space, new line from your css script. In this way it save few bytes of css file and file loaded fast than normal load.

header('Content-type: text/css');
function compress($buffer) {
/* remove comments */
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
/* remove tabs, spaces, newlines, etc. */
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
return $buffer;

/* your css files */


Technique 03:

Most common and super simple techniques for beginner. Create a style.css.php file and write following codes in there.

if(extension_loaded('zlib')){ob_start('ob_gzhandler');} header("Content-type: text/css");


Share It

Related Posts