1:压缩css
- <?php
- header('Content-type: text/css');
- ob_start("compress");
- function compress($buffer) {
-
- $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
-
- $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
- return $buffer;
- }
-
-
- 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'));
|