EMLOG无插件实现网站源码压缩
话不多说,上代码
1、将以下代码放在模版的module.php中:
<?php //全站代码压缩 function em_compress_html_main($buffer){ $initial=strlen($buffer); $buffer=explode("<!--em-compress-html-->", $buffer); $count=count ($buffer); for ($i = 0; $i <= $count; $i++){ if (stristr($buffer[$i], '<!--em-compress-html no compression-->')){ $buffer[$i]=(str_replace("<!--em-compress-html no compression-->", " ", $buffer[$i])); }else{ $buffer[$i]=(str_replace("\t", " ", $buffer[$i])); $buffer[$i]=(str_replace("\n\n", "\n", $buffer[$i])); $buffer[$i]=(str_replace("\n", "", $buffer[$i])); $buffer[$i]=(str_replace("\r", "", $buffer[$i])); while (stristr($buffer[$i], ' ')) { $buffer[$i]=(str_replace(" ", " ", $buffer[$i])); } } $buffer_out.=$buffer[$i]; } $final=strlen($buffer_out); $savings=($initial-$final)/$initial*100; $savings=round($savings, 2); $buffer_out.="\n<!--压缩前的大小: $initial bytes; 压缩后的大小: $final bytes; 节约:$savings% -->"; return $buffer_out; } ?>
2、以下代码是扔在footer.php最末尾(即</html>结尾处)
<?php $html=ob_get_contents(); ob_get_clean(); echo em_compress_html_main($html); ?>
3、如果要实现不压缩pre中的代码,要在module.php中加入下面代码:
<?php //不压缩pre function unCompress($content){ if(preg_match_all('/(crayon-|<\/pre>)/i', $content, $matches)) { $content = '<!--em-compress-html--><!--em-compress-html no compression-->'.$content; $content.= '<!--em-compress-html no compression--><!--em-compress-html-->'; } return $content; } unCompress($log_content); ?>
4、然后把文章页面(echo_log.php)文件中的$log_content用下面代码替换:
unCompress($log_content)
效果图:
发表评论