PHP压缩CSS文件示例代码
用PHP以一种简便的方式来压缩你的CSS文件,不需要命名你的.css文件和.php文件。
当前有许多方法需要将.css文件重命名成.php文件,然后在所有PHP文件中放置压缩代码。
本代码的优点:
不需要重命名CSS并且只需要一个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();
?>
此使用了output buffer函数来实现,此函数说明,请参考:Output Buffer Explained(http://www.phpcodebase.com/php-output-buffering-explained/)。
您可能感兴趣的文章:
PHP压缩CSS文件示例代码
php 压缩多个CSS文件的实现代码
PHP开启gzip压缩的二种方法
php压缩与解压缩类PclZip的例子
php压缩函数(gzcompress gzuncompress)压缩字符串
asp.net中如何用GZip压缩和解压
PHP 压缩字符串的几种方法
php zip解压缩类pclzip用法举例
教你修改Apache配置文件开启gzip压缩传输
php 字符串压缩方法对比分析