php压缩多个CSS/JS文件

1:压缩css

  1. <?php
  2. header('Content-type: text/css');
  3. ob_start("compress");
  4. function compress($buffer) {
  5. /* remove comments */
  6. $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
  7. /* remove tabs, spaces, newlines, etc. */
  8. $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer);
  9. return $buffer;
  10. }
  11. /* your css files */
  12. include('galleria.css');
  13. include('articles.css');
  14. ob_end_flush();

使用:

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

2:压缩js,利用jsmin类:

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

  1. header('Content-type: text/javascript');
  2. require 'jsmin.php';
  3. echo JSMin::minify(file_get_contents('common.js') . file_get_contents('common2.js')); //phpfensi.com