首页 >php教程 >php手册 >php压缩多个CSS/JS文件

php压缩多个CSS/JS文件

WBOY
WBOY原创
2016-05-26 08:21:00866浏览

1:压缩css

<?php   
header(&#39;Content-type: text/css&#39;);   
ob_start("compress");   
function compress($buffer) {   
/* remove comments */   
$buffer = preg_replace(&#39;!/*[^*]**+([^/][^*]**+)*/!&#39;, &#39;&#39;, $buffer);   
/* remove tabs, spaces, newlines, etc. */   
$buffer = str_replace(array("rn", "r", "n", "t", &#39;  &#39;, &#39;    &#39;, &#39;    &#39;), &#39;&#39;, $buffer);   
return $buffer;   
}     
   
/* your css files */   
include(&#39;galleria.css&#39;);   
include(&#39;articles.css&#39;);
ob_end_flush();

使用:

<link href="compress.php" rel="stylesheet" type="text/css" /><span id="tester">test</span>

2:压缩js,利用jsmin类:

来源:http://code.google.com/p/minify/

header(&#39;Content-type: text/javascript&#39;);   
require &#39;jsmin.php&#39;;   
echo JSMin::minify(file_get_contents(&#39;common.js&#39;) . file_get_contents(&#39;common2.js&#39;));  //phprm.com

                               


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn