学习、分享、快乐
当前位置 >> 68楼学习网学习教育电脑学习编程入门PHP教程使用PHP来压缩CSS文件

使用PHP来压缩CSS文件

[08-23]   来源:http://www.68lou.com  PHP教程   阅读:134

使用PHP来压缩CSS文件,本站还有更多关于PHP教程,PHP技巧,php培训,php学习,php安装的文章。
正文:

减少CSS文件的大小能有效减少页面的加载时间,改善用户体验。本文将介绍一种利用php来压缩CSS文件的方法。

新建css-compress.php文件
 

php 代码复制内容到剪贴板
  1. header('Content-type: text/css');   
  2. ob_start("compress");   
  3. function compress($buffer) {   
  4. /* remove comments */  
  5. $buffer = preg_replace('!/*[^*]**+([^/][^*]**+)*/!', '', $buffer);   
  6. /* 清楚 tabs, 空格, 新行, 等. */  
  7. $buffer = str_replace(array("rn", "r", "n", "t", ' ', ' ', ' '), '', $buffer);   
  8. return $buffer;   
  9. }   
  10.   
  11. /* 所有的CSS文件 */  
  12. include('master.css');   
  13. include('typography.css');   
  14. include('grid.css');   
  15. include('print.css');   
  16. include('handheld.css');   
  17.   
  18. ob_end_flush();   
  19.   

使用方法


1. 在浏览器中打开上面的 css-compress.php 文件,即可输出压缩后的CSS源代码,保存即可。【推荐】
2. 直接调用css-compress文件,代码如下:【不推荐】

<link rel="stylesheet" href="css-compress.php" type="text/css" media="all">




如果觉得使用PHP来压缩CSS文件不错,可以推荐给好友哦。
Tag:PHP教程PHP技巧,php培训,php学习,php安装电脑学习 - 编程入门 - PHP教程
相关文章