Home >Backend Development >PHP Tutorial >PHP tips: How to use the JS and CSS optimization tool Minify_PHP tutorial
1. Code to merge and compress multiple JS and CSS files
HTML:
PHP:
$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"
3. Just quote it in the web page as follows:
The number aftercan 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