<?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 M Y H: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);
?>
此代碼還兼容用於合併JavaScript的
當然這是可能的。爲什麼它不適合你?沒有錯誤?沒有? – Brad 2012-03-25 18:44:32
什麼「不起作用」是什麼意思?當你直接從瀏覽器訪問'css_loader.php'並保存我們的成果時,你看到了什麼? – Yaniro 2012-03-25 18:45:08
你是如何將它應用到你的頁面的?已經輸出到瀏覽器後,標題不起作用。另外CSS需要放在'
'部分而不是body中,所以它對你如何應用這一點有所不同。 – sdjuan 2012-03-25 18:46:11