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("rn", "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')); //phprm.com