PHP教程
这里将介绍应用PHP以一种简便的方法来紧缩你的CSS文件。这种办法不须要定名你的.css文件和.php文件。
当前有很多办法须要将.css文件重定名成.php文件,然后在所有PHP文件中放置紧缩代码。如今介绍这种技能,不须要重定名CSS并且只须要一个PHP文件就能搞定。
那让我们看看是怎么实现,起首创建一个PHP文件,然后将下面的代码放到刚创建的PHP文件中。
PHP代码:这个办法应用了output buffer函数来实现
// 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) ;