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

php压缩多个CSS/JS文件

WBOY
WBOYOriginal
2016-05-26 08:21:00887browse

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

                               


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn