I. Implementing code to merge and compress multiple JS and CSS files
Html:
Copy Code code as follows:
<link rel= "stylesheet" type= text/css "href=" cssmin.php?get=base,style1,style2,global&path=css/&v= 20131023 "/>
<script type= "Text/javascript" src= "Jsmin.php?get=jquery-1.6.4.min.js,minjquery.js,minjquery.ui.js,test.js, global.js&path=js/&v=20131023 "></script>
Php:
Copy Code code as follows:
Output JS
Header ("Content-type:application/x-javascript; Charset:utf-8 ");
if (Isset ($_get)) {
$files = Explode (",", $_get[' get ']);
$str = ';
foreach ($files as $key => $val) {
$str. = file_get_contents ($_get[' path '). $val);
}
$str = Str_replace ("T", "", $str); Clear spaces
$str = Str_replace ("\ r \ n", "", $str);
$str = Str_replace ("\ n", "", $str);
Delete Single-line Comment
$str = Preg_replace ("/\/\/\s*[a-za-z0-9_\x7f-\xff][a-za-z0-9_\x7f-\xff]*/", "", $str);
Delete Multiline comments
$str = Preg_replace ("/\/\*[^\/]*\*\//s", "", $str);
Echo $str;
}
Output CSS
Header ("CONTENT-TYPE:TEXT/CSS; Charset:utf-8 ");
if (Isset ($_get)) {
$files = Explode (",", $_get[' get ']);
$FC = ';
foreach ($files as $key => $val) {
$FC. = file_get_contents ($_get[' path '). $val. ". CSS ");
}
$FC = Str_replace ("T", "", $FC); Clear spaces
$FC = Str_replace ("\ r \ n", "", $FC);
$FC = Str_replace ("\ n", "", $FC);
$FC = Preg_replace ("/\/\*[^\/]*\*\//s", "", $FC);
Echo $FC;
}
Just a simple prototype, no encapsulation. In addition, the merged files remember to match the cache
Second, the use method of Minify
1, download the latest version from the code.google.com/p/minify/minify and decompression, the "Min" folder together with the contents of the content copied to the Document_root directory (that is, sites and directories).
You can modify the folder name "Min"
2. Configure the G parameter in "min/groupsconfig.php"
Copy Code code as follows:
Return Array (
' JS ' => array ('//js/file1.js ', '//js/file2.js '),
' CSS ' => array ('//css/file1.css ', '//css/file2.css '),
);
3, in the Web page according to the following way to reference it:
<script type= "Text/javascript" src= "/min/g=js&20140519" ></script>
The following numbers can be marked with an updated date, and the "Min" and the name in step 1 correspond.
4, performance optimization, please refer to Code.google.com/p/minify/wiki/cookbook
Attention:
1, need to httpd.conf in the Rewrite_module module open
2, the development process, you can debug mode open, after the development of the debug mode will be closed, you can use the Firefox browser firebug to view
Copy Code code as follows:
$min _allowdebugflag = True