<?php // Array of css files $css = array( 'first.css', 'second.css' ); $mergeCSS = ""; // Loop the css Array foreach ($cssas $css_file) { // Load the content of the css file $mergeCSS.= file_get_contents($css_file); } // Remove comments also applicable in javascript $mergeCSS= preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $mergeCSS); // Remove space after colons $mergeCSS= str_replace(': ', ':', $mergeCSS); // Remove whitespace $mergeCSS= str_replace(array("\n", "\t", ' ', ' ', ' '), '', $mergeCSS); //Generate Etag $genEtag = md5_file($_SERVER['SCRIPT_FILENAME']); // call the browser that support gzip, deflate or none at all, if the browser doest support compression this function will automatically return to FALSE ob_start('ob_gzhandler'); // call the generated etag header("Etag: ".$genEtag); // Same as the cache-control and this is optional header("Pragma: public"); // Enable caching header("Cache-Control: public "); // Expire in one day header('Expires: ' . gmdate('D, d MYH:i:s', time() + 86400 ) . ' GMT'); // Set the correct MIME type, because Apache won't set it for us header("Content-type: text/javascript"); // Set accept-encoding header('Vary: Accept-Encoding'); // Write everything out echo($mergeCSS); ?>
This code is also compatible for merging javascript
John louie biñas
source share