Php compression HTML functions can be easily compressed html/js/Css and precautions

Source: Internet
Author: User
How to improve the loading speed of web pages requires the optimization of web pages, and so on, are the concerns of webmasters. In fact, there are many ways to compress web pages, this article will explain how php compresses HTML functions to easily compress htmljsCss. if you are interested, I hope this article will help you. 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:
The 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 (
"/> * ([^] *) * "/[\ 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.
The 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, and remove comment
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;
}

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.
Related Article

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.