php 页面压缩---

网站优化中,页面压缩是很有效的一种方法,可以明显提升页面访问速度。

页面压缩也有很多的方法,有PHP自带的zlib的gzip压缩,还有清除html页面中不必要的字符,空格,注释,换行符等。

第一种打开PHP.ini中的配置既可以,可以参考:

http://mp.blog.csdn.net/postedit/79265316php压缩css.js文件

优化网站先试用了第二种,代码:

[php] view plain copy
 
  1. //压缩 html....
    $page_html = str_replace(" ", '', $content); //清除换行符
    $page_html = str_replace(" ", '', $page_html); //清除换行符
    $page_html = str_replace(" ", '', $page_html); //清除制表符
    $pattern = array(
    "/> *([^ ]*) *</", //去掉注释标记
    "/[s]+/",
    "/<!--[^!]*-->/",
    "/" /",
    "/ "/",
    "'/*[^*]**/'",
    "'/*{1,2}[sS]*?*/'", //去除 /** 123 */ 这样的注释

    );
    $replace = array(">\1<", " ", "", """, """, "", "");
    $content = preg_replace($pattern, $replace, $page_html);

压缩结果还是很明显的。

压缩前:
压缩后:
希望能帮助到需要的人,如有大神有更好的方法,欢迎交流,指教(qq:524277477,微信:13263412524) 
 
转载自 https://blog.csdn.net/qq_19448277/article/details/79265568
原文地址:https://www.cnblogs.com/whm-blog/p/8992053.html