压缩html 减小存储空间

发布时间:2025-12-10 11:28:44 浏览次数:4

压缩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);}

 

方法二、使用gzip进行压缩,存储在硬盘中,设置默认header头为gzip,则浏览器会解压。

  1.将html文件进行gzip压缩存储。

  2.设置服务器属性,分 iis 和 apache。    

     如果站点本身就开了(html,htm)的gzip,则会出现 

        html本地未压缩        (html本地正常→html服务器自动压缩→传给浏览器→浏览器解压→得到的是(正常页面))

                    本地已经压缩好的html  (html本地已压缩→服务器压缩→传给浏览器→浏览器解压→得到的是(html本地已压缩)) 

              所以我们生成的静态页面,已经gzip压缩存于本地的,最好换个后缀如htmls 或其它。

                                                    (html本地已压缩→传给浏览器→浏览器解压→得到的是政策也没) 没有了服务器压缩过程。

   1)IIS设置默认header头,Content-Encoding:gzip 

      打开iis→指定网站→指定文件夹→右键属性, http头, 添加 Content-Encoding:gzip

                                                      选择mime类型,添加自定义后缀。.后缀:htmls  类型:text/html

        2) apache服务器添加配置      

       在httpd.conf中 搜索AddType 

 

          在<IfModule mime_module>

 

              TypesConfig conf/mime.types

 

            </IfModule>

 

          模块之中加入

 

                AddEncoding x-gzip .htmls

 

                AddType text/html .htmls

 

转载于:https://www.cnblogs.com/gamir/p/3467358.html

需要做网站?需要网络推广?欢迎咨询客户经理 13272073477