zoukankan      html  css  js  c++  java
  • 压缩html 减小存储空间

    压缩html 减小存储空间


    方法一
    、php代码,清除换行符,清除制表符,去掉注释标记 

            /** 
    	* 压缩html : 清除换行符,清除制表符,去掉注释标记 
    	* @param $string 
    	* @return压缩后的$string
    	* */ 
    	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);
    	}
    

     

    方法二、使用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

     

    牧羊童Gamir——随遇而安,保持一颗愉快之心!
  • 相关阅读:
    codeforces 519E A and B and Lecture Rooms lca倍增
    codeforces 702E Analysis of Pathes in Functional Graph 倍增
    hdu 5126 stars cdq分治套cdq分治+树状数组
    hdu 5442 Favorite Donut 最大表示法+kmp
    hdu 5446 Unknown Treasure 中国剩余定理+lucas
    hdu 5769 Substring 后缀数组
    codevs 1913 数字梯形问题 费用流
    Python for Infomatics 第13章 网页服务二(译)
    Python for Infomatics 第13章 网页服务一(译)
    Python for Infomatics 第12章 网络编程六(译)
  • 原文地址:https://www.cnblogs.com/gamir/p/3467358.html
Copyright © 2011-2022 走看看