PHP compression HTML function Easy to achieve compression html/js/css and precautions _php Tutorial

Source: Internet
Author: User
Tags browser cache
causes of compressed HTML
How to improve the page loading speed, how to optimize the HTML page to believe that each proposed to improve the station technology webmaster had thought of the problem, in fact, the method of Web optimization is still a lot.

There are children's shoes to ask Higrid how to compress HTML, that is, you can all the HTML, JS, CSS before running all compressed into a line, clear comment tags, line breaks, spaces, tabs and so on. A direct benefit of this is to reduce the volume of HTML pages to improve front-end loading speed. Many people think of starting gzip, but generally starting gzip is less on HTML boot gzip compression, because now the HTML is dynamic, do not use the browser cache, and enable gzip every time the request is compressed, will be compared to consume server resources, to JS, CSS startup gzip is good because JS,CSS will use caching. And we also use a lot of software filtering compression, there are online js/css/html compression tools, Higrid feel also very troublesome, poor readability. Higrid that if the compression function is made into a function, so that developers see an uncompressed state, but when visitors visit, the service side of the program to compress the HTML page, clear comment tags, line breaks, spaces, tabs and so on to reduce the HTML volume purposes. If you frequently visit higrid.net, right-click on the HTML source code, you will see this HTML source code has been compressed processing. Including the free content management system provided by Higrid.net, the output is compressed HTML, stripped of whitespace, line breaks, tabs. However, there are some exceptions to the higrid.net, that is, higrid.net mainly recommend online forms, online graphics, including jquery These demos, for the convenience of visitors, there is no boot compression.

Therefore, Higrid personally think that the greatest advantage of compressing HTML is discriminating ears, as long as the function is written once, and later in the need to use the call on it, all programs can be used, will not add any additional development work. Today Higrid to everyone to share a few people feel good function, please try it, I believe you will like.

use PHP to compress HTML
Because Higrid is interested in PHP, using PHP to compress HTML, of course, the use of other languages is similar, for example, using ASP to compress HTML, the same should be the case.

Higrid will compress the function of HTML with PHP written a function, in fact, such as PHP compression function on the Internet a lot, do not believe you can Baidu or Google, but most are not very useful, especially in compression JS or compression CSS, the main reason is that some compression comments and other aspects of the problem caused by different. First look at this function:
Copy CodeThe code is as follows:
/**
* Compress HTML: Clear line break, clear tab, remove comment mark
* @param $string
* The $string after @return compression
* */
function compress_html ($string) {
$string =str_replace ("\ r \ n", ", $string);//Clear line break
$string =str_replace ("\ n", "', $string);//Clear line break
$string =str_replace ("\ T", "', $string);//Clear tabs
$pattern =array (
"/> * ([^]*) * "/[\s]+/",
"/ /",
"/\" /",
"/ \"/",
"'/\*[^*]*\*/'"
);
$replace =array (
">\\1<",
" ",
"",
"\"",
"\"",
""
);
Return Preg_replace ($pattern, $replace, $string);
}

PHP to compress HTML considerations
The way PHP is implemented by compressing the HTM is mainly to use regular expressions to find, replace. In the HTML compression, the main points to note the following:

In a 1.HTML document, multiple whitespace characters are equivalent to a single white space character. This means that the deletion of whitespace characters such as line breaks is unsafe and may cause differences in the style of some elements.
There is a pre in 2.html that represents preformatted text. Any blank inside can not be deleted, so the content format inside the Pre,textarea tag needs to be preserved and cannot be compressed.
There may be an IE conditional comment in 3.HTML. These conditional comments are part of the document's logic and cannot be deleted. As a result, some annotations are not removed when HTML annotations are removed, such as:
4. Compress the comments in the embedded JS note, because the possible annotation symbols will appear in the string, such as: var url = "Http://www.higrid.net"; The front//is not a comment
5. For dynamic pages, HTML compression may also increase the CPU load on the server.

PHP Compressed HTML function code used by Higrid
Because the comment is useful to the code, the PHP compressed HTML function code used by Higrid does not remove the comment, directly on the code.
Copy CodeThe code is as follows:
function higrid_compress_html ($higrid _uncompress_html_source)
{
$chunks = Preg_split ('/( )/ms ', $higrid _uncompress_html_source,-1, preg_split_delim_capture);
$higrid _uncompress_html_source = ';//[higrid.net] Modify compressed HTML: Clear line breaks, clear tabs, remove comment marks
foreach ($chunks as $c)
{
if (Strpos ($c, ' {
[Higrid.net] Remove new lines & tabs
$c = preg_replace ('/[\\n\\r\\t]+/', ' ', $c);
[higrid.net] Remove extra whitespace
$c = preg_replace ('/\\s{2,}/', ' ', $c);
[Higrid.net] Remove Inter-tag whitespace
$c = Preg_replace ('/>\\s < ', $c);
[Higrid.net] Remove CSS & JS comments
$c = preg_replace ('/\\/\\*.*?\\*\\//i ', ' ', $c);
}
$higrid _uncompress_html_source. = $c;
}
return $higrid _uncompress_html_source;
}

PHP Compressed HTML function code Summary
Some children's shoes do not recommend compressing HTML, the main reason in addition to the above mentioned PHP to compress HTML considerations, through GZIP compression has been able to achieve good results. In addition, because there are too many roles to affect HTML (static, dynamic, front-end dynamic), there is no quantitative indicator, so it is difficult to control what the compression (code written to what extent). The code needs to consider execution efficiency rather than transmission efficiency. For dynamic pages, HTML compression may also increase the CPU load on the server. Google's compressed Web page is because he wanted the homepage text to be in one or two packages as much as possible, and his home page was too important and the traffic was outrageous. Compression of a byte, the total traffic is a small number, nature is also necessary. There is a problem with further compression, and it is not recommended to compress HTML unless you can test it as well as Google (Google also compresses only a few of the core Services pages).

But using higrid.net PHP compressed HTML function code, can be a good solution to this problem. Well, try it again.

http://www.bkjia.com/PHPjc/326529.html www.bkjia.com true http://www.bkjia.com/PHPjc/326529.html techarticle the cause of compressed HTML How to improve the page loading speed, how to optimize the HTML page to believe that every proposed to improve the station technology webmaster had thought of the problem, in fact, the method of Web optimization is still very ...

  • 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.