PHP zip html web code of clear Spaces newlines tabs comment marks

  • 2020-05-16 06:34:50
  • OfStack

PHP compresses html web code (clear Spaces, newlines, tabs, comment tags).
A good way to do this is to compress HTML. To compress html is to clean up the newline, clean up the tabs, and clean up the comments. The role it plays should not be underestimated.
The PHP compression HTML function is now provided. Please give it a try. It feels good.

No more nonsense, directly on the code:
 
<?php 
/** 
*  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  
$pattern = array ( 
"/> *([^ ]*) *</", // Uncomment markup  
"/[\s]+/", 
"/<!--[^!]*-->/", 
"/\" /", 
"/ \"/", 
"'/\*[^*]*\*/'" 
); 
$replace = array ( 
">\\1<", 
" ", 
"", 
"\"", 
"\"", 
"" 
); 
return preg_replace($pattern, $replace, $string); 
} 
?> 

Related articles: