PHP压缩HTML代码

乱编乱写 2959 Views 0 Comments

PHP压缩HTML代码

<?php
function compress_html($string){ 
$string=str_replace("\r\n",'',$string);//清除换行符 
$string=str_replace("\n",'',$string);//清除换行符 
$string=str_replace("\t",'',$string);//清除制表符 
$pattern=array(
"/> *([^ ]*) *</",//去掉注释标记 
"/[\s]+/", 
"/<!--[^!]*-->/", 
"/\" /", 
"/ \"/", 
"'/\*[^*]*\*/'" 
); 
$replace=array (
">\\1<", 
" ", 
"", 
"\"", 
"\"", 
"" 
);
return preg_replace($pattern, $replace, $string); 
} 
?>

或者也可以简单写成

<?php
function compress_html($string){
$string = str_replace(array("\r\n","\n","\t",'  '),'',$string); 
return $string;
}
?>


标签: 代码, PHP, HTML,

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
loading