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——随遇而安,保持一颗愉快之心!
  • 相关阅读:
    Property ‘password’ threw Exception
    eclipse中mybatis自动生成插件使用
    应用笔记:Markdown语法记录
    笔记:Code Warrior 问题汇总
    笔记:Altium Designer-电路设计与制作
    笔记:stm32f030 要点总结(时钟、中断、GPIO、定时器、串口、看门狗)
    笔记:搭建pyQT5开发环境(Python3 +PyCharm 2018 +PyQt5)
    06-笔记:LPC1788-定时器
    09-笔记:LPC1788-内置EE
    08-笔记:LPC1788-滴答定时器(系统节拍定时器)
  • 原文地址:https://www.cnblogs.com/gamir/p/3467358.html
Copyright © 2011-2022 走看看