使用PHP來壓縮CSS文件
這里將介紹使用PHP以一種簡便的方式來壓縮你的CSS文件。這種方法不需要命名你的.css文件和.php文件。
當前有許多方法需要將.css文件重命名成.php文件,然后在所有PHP文件中放置壓縮代碼。現在介紹這種技巧,不需要重命名CSS并且只需要一個PHP文件就能搞定。
那讓我們看看是怎么實現,首先創建一個PHP文件,然后將下面的代碼放到剛創建的PHP文件中。
<?php
// This defines the header type
header("Content-type: text/css");
// Start the output buffer
ob_start("compress_css");
// Function which actually compress
// The CSS file
function compress_css($buffer)
{
/ remove comments /
$buffer = preg_replace("!/*[^]*+([^/][^]*+)/!", "", $buffer) ;
/ remove tabs, spaces, newlines, etc. */
$arr = array("\r\n", "\r", "\n", "\t", " ", " ", " ") ;
$buffer = str_replace($arr, "", $buffer) ;
return $buffer;
}
/ include all CSS files /
include("style.css");
include("fonts.css");
include("print.css");
// Flush the output buffer
ob_end_flush();
?></pre>這個方法使用了output buffer函數來實現,如果你還不了解這個函數,請看它的說明
Output Buffer Explained。
本文由用戶 碼頭工人 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!