PHP tips for JS and CSS Optimization tool minify method of use _php skills

Source: Internet
Author: User
Tags explode

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.