php压缩多个CSS/JS文件

1:压缩css

<?php
header('Content-type: text/css');
ob_start("compress");
function compress($buffer) {
	/* remove comments */
    $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    /* remove tabs, spaces, newlines, etc. */
    $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
    return $buffer;
}  

/* your css files */
include('galleria.css');
include('articles.css');

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('Content-type: text/javascript');
require 'jsmin.php';
echo JSMin::minify(file_get_contents('common.js') . file_get_contents('common2.js'));




你可能感兴趣的:(php压缩多个CSS/JS文件)