PHP compressed HTML function Easy to implement compression html/js/css and precautions _php skills

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

Have children's shoes asked Higrid how to compress HTML, that is, can all the HTML, JS, CSS before running all compressed into one line, clear annotation tags, line breaks, spaces, tabs and so on. Such a direct benefit is to reduce the volume of HTML pages to increase the front-end load speed. Many people think that start gzip, but the general boot gzip less on the HTML start gzip compression, because now the HTML is dynamic, will not use browser caching, and enable gzip every request need compression, will be compared to consume server resources, to JS, CSS is better to start gzip because JS,CSS will use caching. And we also use a lot of software to filter the compression, there are online js/css/html compression tools, Higrid feel also very troublesome, poor readability. Higrid thinks that if the compression function is made into a function, the developer will see the uncompressed state, but when the visitor accesses, the server's program compresses the HTML page, clears the annotation tags, line breaks, spaces, tabs, and so on to achieve the goal of reducing the size of the HTML. If you often visit higrid.net, right-click 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, remove the blank, line break, tab. However, there are some exceptions to higrid.net, that is, higrid.net mainly recommend online forms, online graphics, including jquery These demos, for the convenience of visitors, there is no start compression.

Therefore, Higrid personally feel that the biggest advantage of compressed HTML is lucrative, as long as you write a function, in the future when you need to use the call on it, all programs can be used, will not add any additional development work. Today Higrid to share a few personal feel good function, please let us try, I believe we will like.

use PHP to compress HTML
Because Higrid is interested in PHP, so use PHP to compress HTML, of course, in other languages, such as using ASP to compress HTML, the reason should be the same.

Higrid will compress the HTML function to write a function in PHP, in fact, such PHP compression function on the Internet is also a lot, do not believe you can Baidu or Google, but most are not very good, especially in the compression of JS or compressed CSS, the main reason is some compression notes and other aspects of the problem caused. Look at this function first:
Copy Code code as follows:

/**
* Compress HTML: Clear line breaks, clear tabs, remove comment marks
* @param $string
* @return Compressed $string
* */
function compress_html ($string) {
$string =str_replace ("\ r \ n", ", $string);//Clear line feed
$string =str_replace ("\ n", ", $string);//Clear line breaks
$string =str_replace ("T", ', $string);/Clear Tab
$pattern =array (
"/> * ([^]*) *</",//Remove comment marks
"/[\s]+/",
"/<!--[^!] *-->/",
"/\" /",
"/ \"/",
"'/\*[^*]*\*/'"
);
$replace =array (
">\\1<",
" ",
"",
"\"",
"\"",
""
);
Return Preg_replace ($pattern, $replace, $string);
}

PHP to compress HTML considerations
PHP to compress the HTM implementation of the main way is to use regular expressions to find, replace. In HTML compression, the main attention to the following points:

In a 1.HTML document, multiple whitespace characters are equivalent to one white space character. That is, the deletion of whitespace characters, such as newline, is unsafe and may result in differences in the styles of some elements.
There is a pre in 2.html that represents preformatted text. Any white space inside can not be deleted, so the content format of the Pre,textarea tag needs to be preserved and cannot be compressed.
There may be an IE condition annotation in 3.HTML. These conditional comments are part of the document's logic and cannot be deleted. So when you get rid of HTML annotations, some comments can't be removed, such as:
4. Compress embedded JS in the comments to note, because the possible annotation symbol will appear in the string, such as: var url = "Http://www.higrid.net"; Front//is not a comment
5. For dynamic pages, HTML compression may also increase the CPU burden on the server, outweigh

PHP compression HTML function code used by Higrid
Because the annotation is useful for the code, the PHP compression HTML function code used by Higrid does not remove the annotation, directly on the code.
Copy Code code as follows:

function higrid_compress_html ($higrid _uncompress_html_source)
{
$chunks = Preg_split (' <pre.*?\/pre>)/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, ' <pre ')!== 0)
{
[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 Compression HTML Function Code Summary
Some children's shoes do not recommend compressed HTML, the main reason in addition to the above mentioned PHP to compress HTML considerations, through GZIP compression has been able to achieve very good results. In addition, because there are too many roles to influence HTML (static, dynamic, front-end dynamic), there is no quantitative indicator, so it is difficult to control what compression is (how much code is written). The code needs to consider execution efficiency rather than transmission efficiency. For dynamic pages, the compression of HTML may also increase the CPU burden on the server, outweigh the gains. Google's compressed Web page is because early on he wanted the homepage text to be as controlled as possible in one or two packages, and his home page was too important and the traffic was outrageous. Compressed a byte, the total flow is a small number, nature is necessary to lift. The problem with further compression is that it is not recommended to compress HTML unless it is tested as fully as Google (Google also compresses only a few of its core services).

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

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.