zoukankan      html  css  js  c++  java
  • php压缩html代码减少页面响应时间

    function compress_html($string) {
        $string = str_replace(" ", '', $string); //清除换行符 
        $string = str_replace(" ", '', $string); //清除换行符 
        $string = str_replace(" ", '', $string); //清除制表符 
        $pattern = array(
            "/> *([^ ]*) *</", //去掉注释标记 
            "/[s]+/",
            "/<!--[^!]*-->/",
            "/" /",
            "/ "/",
            "'/*[^*]**/'"
        );
        $replace = array(
            ">\1<",
            " ",
            "",
            """,
            """,
            ""
        );
        return preg_replace($pattern, $replace, $string);
    }
     
     
    $content = preg_replace("~>s+<~","><",preg_replace("~>s+ ~",">",$html));//保留js
    $content=preg_replace('#<!--[^![]*?(?<!//)-->#' , '' , $content);//去掉代码中注释
    $content = ltrim(rtrim(preg_replace(array("/> *([^ ]*) *</","//","'/*[^*]**/'","/ /","/ /","/ /",'/>[ ]+</'),array(">\1<",'','','','','','><'),$content)));
    $html=compress_html($content);
    return $html;
  • 相关阅读:
    使用JavaScript获取select元素选中的value和text
    EF应用一:Code First模式
    EF常用查询语句
    在EF中执行SQL语句
    c++学习笔记之继承篇
    pyqt系列原创入门教程
    python sorted排序用法详解
    常见排序算法-Python实现
    作品集
    deepin系统如何安装deb格式的软件
  • 原文地址:https://www.cnblogs.com/wenxinphp/p/12889860.html
Copyright © 2011-2022 走看看