Causes of HTML Compression
How to Improve the loading speed of web pages, how to optimize html pages is believed to be a problem that every webmaster intends to improve the website construction technology has previously thought of. In fact, there are still many ways to optimize web pages.
Some children's shoes asked higrid how to compress HTML. That is to say, can we compress all html, js, and Css into one line before running and clear comments, line breaks, spaces, and tabs. The direct advantage of this is to reduce the size of html pages to increase the front-end loading speed. Many people think that gzip is enabled, but generally, gzip is rarely started for html, because the current html is dynamic and does not use browser cache, when gzip is enabled, each request needs to be compressed, which will consume server resources. It is better to start gzip for js and css because js and css will use cache. While we have also used a lot of software to filter compression, and there are also online js/css/html compression tools. higrid feels very troublesome and has poor readability. Higrid believes that if the compression function is made into a function, the developer will see the uncompressed state, but when the visitor accesses the server, the program will compress the html page, clear comments, line breaks, spaces, and tabs to reduce the size of html. If you frequently access higrid.net, right-click the html source code and you will see that the html source code in this article has been compressed. This includes the free content management system provided by higrid.net, which compresses html and removes blank spaces, line breaks, and tabs. However, there are some exceptions in higrid.net, that is, higrid.net mainly recommends online tables and online graphics, including jquery demos. For the convenience of visitors, compression is not enabled.
Therefore, higrid personally thinks that the biggest benefit of html compression is a benefit. As long as you have written the compile function, you can call it when you need to use it. All programs can be used, no additional development work will be added. Today, higrid will share with you a few functions that I personally think are useful. Please try them out. I believe you will like them.
Use php to compress HTML
Because higrid is interested in php, php is used to compress HTML. Of course, it is similar to other languages. For example, if asp is used to compress HTML, the principle should be the same.
Higrid uses php to write the html compression function into a function. In fact, there are many php compression functions on the Internet. If you don't believe it, you can use Baidu or Google, but most of them are not very useful, especially when compressing js or CSS, the main cause is that some different compression comments may cause problems. First look at this function:Copy codeThe Code is as follows :/**
* Html compression: clear line breaks, clear tabs, and remove comment tags
* @ Param $ string
* @ Return $ string after compression
**/
Function compress_html ($ string ){
$ String = str_replace ("\ r \ n", '', $ string); // clear line breaks
$ String = str_replace ("\ n", '', $ string); // clear the line break
$ String = str_replace ("\ t", '', $ string); // clear the tab
$ Pattern = array (
"/> * ([^] *) * </", // Remove the comment mark
"/[\ S] + /",
"/<! -- [^!] * --> /",
"/\"/",
"/\"/",
"'/\ * [^ *] * \ */'"
);
$ Replace = array (
">\\ 1 <",
"",
"",
"\"",
"\"",
""
);
Return preg_replace ($ pattern, $ replace, $ string );
}
Considerations for compressing HTML with php
Php is used to compress and replace HTM with regular expressions. When compressing html, pay attention to the following points:
1. In an HTML document, multiple blank characters are equivalent to one blank character. That is to say, it is unsafe to delete blank characters such as line breaks, which may lead to different styles of some elements.
2.html contains a pre, indicating that any blank space in preformatted text. cannot be deleted. Therefore, the content format in the pre and textarea labels must be retained and cannot be compressed.
3. There may be IE conditional comments in HTML. These condition annotations are part of the document logic and cannot be deleted. Therefore, some annotations cannot be removed when html annotations are removed, for example:
4. Note the need to compress comments in Embedded js, because the comments may appear in the string, such as: var url = "http://www.higrid.net"; // The previous // not the comments
5. For dynamic pages, HTML compression may also increase the server's CPU burden.
Php compression html function code used by higrid
Because annotations are useful for the code, the php compression html function code used by higrid does not remove the annotations and directly uploads the code.Copy codeThe Code is 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, and remove comment
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;
}
Summary of php compressed html Function Code
Some children's shoes do not recommend html compression. The main reason is that, in addition to the above mentioned considerations for HTML compression, gzip compression can achieve good results. In addition, because there are too many roles that affect HTML (static, dynamic, and front-end dynamics) and there are no quantitative indicators, it is difficult to control the compression to what extent (code writing ). The code should consider the execution efficiency rather than the transmission efficiency. For dynamic pages, HTML compression may also increase the server's CPU burden. Google used to compress the webpage because he wanted the homepage text to be controlled in one or two packages as much as possible in the early days. In addition, his homepage was too important and the traffic was outrageous. To compress a byte, the total traffic is a small number, which is naturally necessary. Further compression is problematic. Unless it can be fully tested like Google (Google only compresses pages of a few core services), HTML compression is not recommended.
However, using higrid.net's php to compress html function code can solve this problem well. Okay. Try again later.