Home >Backend Development >PHP Tutorial >Web page background music code PHP compresses html web page code to remove spaces, newlines, tabs, and comment marks
PHP compresses html web page code (remove spaces, newlines, tabs, comment marks).
A good method is to compress HTML. Compressing HTML actually means: clearing newlines, clearing tabs, and removing comment marks. The role it plays cannot be underestimated.
Now provides PHP compressed HTML function. Please give it a try, it feels good.
No more nonsense, let’s go straight to the code:
Copy the code The code is as follows:
/**
* Compress html: clear newlines, clear tabs, remove comment marks
* @param $string
* @return compressed $string
**/
function compress_html($string) {
$string = str_replace( "rn", '', $string); //Clear newline characters
$string = str_replace("n", '', $string); //Clear newline characters
$string = str_replace("t", '' , $string); //Clear tab characters
$pattern = array (
"/> *([^ ]*) *", //Remove comment marks
"/[s]+/",
"//",
"/" /",
"/ "/",
"'/*[^*]**/'"
) ;
$replace = array (
">\1<",
" ",
"",
""",
""",
""
);
return preg_replace($pattern, $replace, $string);
}
?>
The above introduces the web page background music code. PHP compresses the html web page code to remove spaces, newlines, tabs, and comment marks, including the web page background music code. I hope it will be helpful to friends who are interested in PHP tutorials.