Example of PHP compression between javascrui and CSS

Source: Internet
Author: User
Tags foreach md5 mkdir php file

First, put all CSS in a directory, and then create an empty CSS file named css. php in this directory (except for the suffix ).

Then put the edge code in the PHP file:

The code is as follows: Copy code

<? Php
Header ('content-type: text/css ');
Ob_start ("compress ");
Function compress ($ buffer ){
$ Buffer = preg_replace ('! /* [^ *] ** + ([^/] [^ *] ** + )*/! ', '', $ Buffer );
$ Buffer = str_replace (array ("rn", "r", "n", "t", ''),'', $ buffer );
Return $ buffer;
}
/* Include('colorpicker.css ');
Include('jquery-ui.css ');
Include('style.css ');
Include('switchery.min.css ');*/
Foreach (glob ("*. css") as $ filename) include $ filename;
Ob_end_flush ();

The code for introducing the CSS file is replaced with the PHP file, for example:


<Link rel = 'stylesheet 'id = 'style-css' href = '/css. Php' type = 'text/css' media = 'all'/>


Compress multiple css into one css

The code is as follows: Copy code

<? Php
/*
Compress multiple CSS files into one and cache for an hour.

Use the same code for Javascript, but replace below "text/css" with "text/javascript" and of course make sure you include. js files instead of. css ones.
*/Www.111cn.net
Ob_start ("ob_gzhandler ");
Header ("Content-type: text/css; charset: UTF-8 ");
Header ("Expires:". gmdate ("D, d m y h: I: s", time () + 60*60). "GMT ");

Include('somefile.css ');
Echo "nn ";
Include('anotherfile.css ');
Echo "nn ";

Ob_flush ();


The following describes the JavaScript and css functions that can be compressed.

The code is as follows: Copy code


/**
* Merge css styles into a file
 *
* @ Param unknown_type $ urls
* @ Param unknown_type $ path
* @ Param unknown_type $ tmpl_path
* @ Return unknown
*/
Function parse_css ($ urls, $ path = "./static/", $ tmpl_path = '.'){
$ Url = md5 (implode (',', $ urls ));
$ Css_url = regular path.20.url.'.css ';
If (! File_exists ($ css_url )){
If (! File_exists ($ path) mkdir ($ path, 0777 );
$ Css_content = '';
Foreach ($ urls as $ url ){
$ Css_content. = @ file_get_contents ($ url );
        }
$ Css_content = preg_replace ("/[rn]/", '', $ css_content );
$ Css_content = str_replace ("../images/", $ tmpl_path. "/images/", $ css_content );
@ File_put_contents ($ css_url, $ css_content );
    }
Return $ css_url;
}
/**
* Merge JavaScript www.111cn.net into a file.
 *
* @ Param unknown_type $ urls
* @ Param unknown_type $ path
* @ Return unknown
*/
Function parse_script ($ urls, $ path = "./static /"){
$ Url = md5 (implode (',', $ urls ));
$ Js_url = $ path. $ url. '. Js ';
If (! File_exists ($ js_url ))
    {
If (! File_exists ($ path) mkdir ($ path, 0777 );
Require_once "inc/javascriptpacker. php ";
$ Js_content = '';
Foreach ($ urls as $ url)
        {
$ Append_content = @ file_get_contents ($ url). "rn ";
$ Packer = new JavaScriptPacker ($ append_content );
$ Append_content = $ packer-> pack ();
$ Js_content. = $ append_content;
        }
@ File_put_contents ($ js_url, $ js_content );
    }
Return $ js_url;
}
Front-end
Js call
<? Php
$ Pagejs [] = $ tplurl. "js/jump. js ";
$ Jsfile = parse_script ($ pagejs, "./template/default/js /",".");
?>
<Script type = "text/javascript" src = "<? = $ Jsfile?> "> </Script>
Css call
<? Php
$ Pagecss [] = $ tplurl. "style/index_top.css ";
$ Pagecss [] = $ tplurl. "style/index.css ";
$ Cssfile = parse_css ($ pagecss, "./template/default/style /",".");
?>
<Link rel = "stylesheet" type = "text/css" href = "<? = $ Cssfile?> "/>

The PHP file contains all the compressed CSS code, and all CSS files in the CSS directory can be automatically introduced without modifying the PHP file when creating the CSS file.

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.