文章来源:
100素材网
更新时间:
2014-08-04 16:33:59
php删除空格 php清除空格 php清除html
为了提高网页加载速度减少打开时间,需要优化网页。网页优化的方法有很多。下面我们通过减小页面体积来提高前端加载速度的方法
PHP压缩html网页的代码(清除空格,换行符,制表符,注释标记)。
压缩html就是:清除换行符,清除制表符,去掉注释标记。
下面是PHP 压缩HTML函数代码。
<?php
/**
* 压缩html : 清除换行符,清除制表符,去掉注释标记
* @param $string
* @return 压缩后的$string
* */
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);
}
?>
浏览次数次


推荐阅读:
