首页 > 开发 > PHP > 正文

php压缩多个CSS/JS文件

2024-05-04 21:59:50
字体:
来源:转载
供稿:网友

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.    
  12. /* your css files */   
  13. include('galleria.css');   
  14. include('articles.css');   
  15.    
  16. 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/

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表