2014-12-13 99 views
2


我試圖通過壓縮Magento的這個生成的HTML代碼:
Observer.php壓縮Magento的HTML代碼

 public function alterOutput($observer) 
    { 
     $lib_path = Mage::getBaseDir('lib').'/Razorphyn/html_compressor.php'; 

     include_once($lib_path); 

     //Retrieve html body 
     $response = $observer->getResponse();  
     $html  = $response->getBody(); 

     $html=html_compress($html); 

     //Send Response 
     $response->setBody($html); 
    } 

html_compressor.php

function html_compress($string){ 

    global $idarray; 
    $idarray=array(); 

    //Replace PRE and TEXTAREA tags 
    $search=array(
        '@(<)\s*?(pre\b[^>]*?)(>)([\s\S]*?)(<)\s*(/\s*?pre\s*?)(>)@', //Find PRE Tag 
        '@(<)\s*?(textarea\b[^>]*?)(>)([\s\S]*?)(<)\s*?(/\s*?textarea\s*?)(>)@' //Find TEXTAREA 
       ); 
    $string=preg_replace_callback($search, 
            function($m){ 
             $id='<!['.uniqid().']!>'; 
             global $idarray; 
             $idarray[]=array($id,$m[0]); 
             return $id; 
            }, 
            $string 
    ); 

    //Remove blank useless space 
    $search = array(
        '@(|\t|\f)[email protected]', // Shorten multiple whitespace sequences 
        '@(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n][email protected]', //Remove blank lines 
        '@^(\s)+|(|\t|\0|\r\n)[email protected]' //Trim Lines 
        ); 
    $replace = array(' ',"\\1",''); 
    $string = preg_replace($search, $replace, $string); 

    //Replace IE COMMENTS, SCRIPT, STYLE and CDATA tags 
    $search=array(
        '@<!--\[if\s(?:[^<]+|<(?!!\[endif\]-->))*<!\[endif\]-->@', //Find IE Comments 
        '@(<)\s*?(script\b[^>]*?)(>)([\s\S]*?)(<)\s*?(/\s*?script\s*?)(>)@', //Find SCRIPT Tag 
        '@(<)\s*?(style\b[^>]*?)(>)([\s\S]*?)(<)\s*?(/\s*?style\s*?)(>)@', //Find STYLE Tag 
        '@(//<!\[CDATA\[([\s\S]*?)//]]>)@', //Find commented CDATA 
        '@(<!\[CDATA\[([\s\S]*?)]]>)@' //Find CDATA 
       ); 
    $string=preg_replace_callback($search, 
            function($m){ 
             $id='<!['.uniqid().']!>'; 
             global $idarray; 
             $idarray[]=array($id,$m[0]); 
             return $id; 
            }, 
            $string 
    ); 

    //Remove blank useless space 
    $search = array(
        '@(class|id|value|alt|href|src|style|title)=(\'\s*?\'|"\s*?")@', //Remove empty attribute 
        '@<!--([\s\S]*?)-->@', // Strip comments except IE 
        '@[\r\n|\n|\r]@', // Strip break line 
        '@[ |\t|\f][email protected]', // Shorten multiple whitespace sequences 
        '@(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n][email protected]', //Remove blank lines 
        '@^(\s)+|(|\t|\0|\r\n)[email protected]' //Trim Lines 
        ); 
    $replace = array(' ','',' ',' ',"\\1",''); 
    $string = preg_replace($search, $replace, $string); 

    //Replace unique id with original tag 
    $c=count($idarray); 
    for($i=0;$i<$c;$i++){ 
     $string = str_replace($idarray[$i][0], "\n".$idarray[$i][1]."\n", $string); 
    } 

    return $string; 
} 

我的主要外觀是兩個:

  • 這是一個沉重的(或好的)解決方案嗎?
  • 有沒有一種方法來優化?
  • 它壓縮Magento HTML頁面(採取資源和時間vs真正的好處)真的有意義嗎?

回答

1

我不會評論或審查您的代碼。破譯regexes(任何味道)不是我最喜歡的愛好。

是的,壓縮 HTML 如果你的目標是提供專業服務是有意義的

如果我查看某人的網站的HTML代碼,裏面有很多無意義的空白空間和用戶無用的評論,並且網站不尊重Google's PageSpeed Insights Rules,並且無助於使網絡更快和更環保,那麼它對我說:要知道,不放心,肯定不會給他們你的信用卡號碼

我的建議是:

  1. 讀這個問題的答案:Stack Overflow: HTML minification?
  2. 閱讀其他開發人員的代碼,這是一個我用:https://github.com/kangax/html-minifier
  3. benchmark,例如,運行Google Chrome > Developer tools > Audits
  4. test很多,如果你minifier不會無意中破壞
0

實在是做這個,如果你有gzip壓縮(你應該有它)沒有啓用點的頁面。這真是浪費CPU週期。您應該專注於圖像優化,減少http請求數量並設置正確的緩存標頭。