Home > Backend Development > PHP Tutorial > PHP tips: How to use the JS and CSS optimization tool Minify_PHP tutorial

PHP tips: How to use the JS and CSS optimization tool Minify_PHP tutorial

WBOY
Release: 2016-07-13 10:29:30
Original
891 people have browsed it

1. Code to merge and compress multiple JS and CSS files

HTML:

Copy code The code is as follows:



PHP:

Copy code The code is 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("rn", "", $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 multi-line 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(" rn", "", $fc);
$fc = str_replace("n", "", $fc);
$fc = preg_replace("//*[^/]**//s ", "", $fc);
echo $fc;
}

Just a simple prototype, no encapsulation. In addition, remember to cache the merged files

2. How to use Minify

1. Download the latest version of Minify from code.google.com/p/minify/ and unzip it. Copy the "min" folder and its contents to the DOCUMENT_ROOT directory (i.e. website and directory).

You can modify the folder name "min"

2. Configure the g parameter in "min/groupsConfig.php"

Copy code The code is as follows:

return array(
// 'js' => array('/ /js/file1.js', '//js/file2.js'),
// 'css' => array('//css/file1.css', '//css/file2.css '),
);

3. Just quote it in the web page as follows:

The number after

can be marked with the update date, and "min" corresponds to the name in step 1.

4. Performance optimization, please refer to code.google.com/p/minify/wiki/CookBook

Note:

1. You need to enable the rewrite_module module in httpd.conf

2. During the development process, you can turn on the debugging mode. After the development is completed, turn off the debugging mode. You can use the firebug of Firefox browser to view

Copy code The code is as follows:

$min_allowDebugFlag = true

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/771875.htmlTechArticle1. Code HTML for merging and compressing multiple JS and CSS files: Copy the code as follows: link rel= "stylesheet" type="text/css" href="cssmin.php?get=base,style1,style2,globalv=20...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template