php compression HTML function easy to achieve compression html and js and Css and considerations

  • 2020-05-27 04:38:01
  • OfStack

Compress the cause of HTML
How to improve the page loading speed, the need to optimize the html page is believed to be every proposed to improve the technical webmaster has thought of the problem, in fact, the web page optimization method is still a lot of.

Some children asked higrid how to compress HTML, that is to say, can all html, js and Css be compressed into one line before running, so as to clear out comment marks, newline characters, Spaces, tabs and so on. One immediate benefit of this is to reduce the size of the html page to increase front-end loading speed. Many people think that launching gzip, but launching gzip like 1 is less likely to start gzip compression for html, because now html is all dynamic and will not use browser cache. If gzip is enabled, every request needs to be compressed, which will consume server resources. It is better to launch gzip for js,css because js and css will use cache. We also use a lot of software to filter 1 compression, there are also online js/css/html compression tools, higrid found it very troublesome, readability is poor. higrid believes that if the compression function is made into one function, the developer will see the uncompressed state. However, when visitors visit the html page, the server program will compress the html page and clear the comment mark, newline, space, TAB, etc. to achieve the goal of reducing the volume of html. If you frequently visit, right-click on the html source code and you will see that this html source code has been compressed. includes a free content management system that compresses the output of html, eliminating white space, newlines, and tabs. With the exception of, mainly recommends online forms and graphics, including jquery. For the convenience of visitors, compression is not enabled.

Therefore, higrid personally thinks that the biggest advantage of compressing html is 10,000 yuan. As long as the function is written once, it can be called once when it needs to be used later. All programs can be used without any additional development work. Today higrid to share with you a few personal feel good functions, please give it a try, I believe you will like.

php is used to compress HTML
Since higrid is interested in php, php is used to compress HTML. Of course, asp is also used to compress HTML in other languages, for example, asp is used to compress HTML.

higrid will compress html function with php to write 1 function, in fact, on the Internet such php compression function is also a lot of, do not believe you can baidu or Google, but most is not very easy to use, especially in the compression of js or compression of CSS, the main reason is 1 some compression notes and other aspects of the different lead to problems. Let's look at this function:
*  The compression html :  Clear the newline character , Clear tabs , Uncomment markup  
* @param $string 
* @return compressed $string 
* */ 
function compress_html($string){ 
$string=str_replace("\r\n",'',$string);// Clear the newline character  
$string=str_replace("\n",'',$string);// Clear the newline character  
$string=str_replace("\t",'',$string);// Clear tabs  
"/> *([^ ]*) *</",// Uncomment markup  
"/\" /", 
"/ \"/", 
$replace=array ( 
" ", 
return preg_replace($pattern, $replace, $string); 

php to compress HTML considerations
php compresses the HTM implementation mainly by using regular expressions to find and replace. When compressing html, the following points should be noted:

1. In HTML documents, multiple white space characters are equivalent to one white space character. That is to say, the deletion of white space characters such as line feed is not safe, which may lead to differences in the style of some elements.
2. There is one pre in html, which means preformatted text. Any blank in pre, text, cannot be deleted, so the content format in pre,textarea labels should be retained, not compressed.
3. It is possible to have IE conditional comments in HTML. These conditional comments are part 1 of the document logic and cannot be deleted. So when you remove the html comments, there are some comments that you can't remove, such as:
4. The compression of the embedded js comments should note that because annotation symbols may appear in the string, such as: var url = "http: / / www. higrid. net"; // the previous // is not a comment
5. For dynamic pages, the compression of HTML may also increase the CPU burden on the server

higrid USES php to compress html function code
Because of the effect of comments on the code, the php compressed html function code used by higrid does not remove the comments and directly enters the code.
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 = '';//[] Modification of the compression html :  Clear the newline character , Clear tabs , Uncomment markup  
foreach ( $chunks as $c ) 
if ( strpos( $c, '<pre' ) !== 0 ) 
//[] remove new lines & tabs 
$c = preg_replace( '/[\\n\\r\\t]+/', ' ', $c ); 
// [] remove extra whitespace 
$c = preg_replace( '/\\s{2,}/', ' ', $c ); 
// [] remove inter-tag whitespace 
$c = preg_replace( '/>\\s</', '><', $c ); 
// [] 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
Compression of html is not recommended for some children's shoes. The main reason is that, in addition to the notice of compression of HTML by php mentioned above, compression of gzip has achieved a good effect. In addition, because there are so many roles (static, dynamic, front-end dynamic) that affect HTML, and there is no quantification, it is difficult to control what is compressed (how much code is written). Code should be more concerned with execution efficiency than transmission efficiency. For dynamic pages, the compression of HTML may also increase the CPU burden on the server. The reason for Google's compression was that early on he wanted to keep the front page text as much as possible in one or two packages, and his front page was so important that traffic was ridiculous. Compress 1 byte, the total traffic 1 calculate is not a small number, naturally also is necessary. There is a problem with the compression in step 1, and unless it can be fully tested like Google 1 (Google also compresses only a few pages of core services), it is not recommended to compress HTML.

However, using php to compress html function code is a good solution to this problem. All right, let's try it.

Related articles: