EMLOG无插件实现网站源码压缩
以下代码是扔在module.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; }以下代码是扔在footer.php最末尾(即</html>结尾处)
<?php if(_g('compress_html')=='open'){ $html=ob_get_contents(); ob_get_clean(); echo em_compress_html_main($html); } ?>以上的代码有一个模板设置判断语句,其代码为以下:
'compress_html' => array( 'type' => 'radio', 'name' => '网站源码压缩', 'description' => '', 'values' => array('open' => '压缩','close' => '关闭'), 'default' => 'open' ),
想要内容里面的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);
独狼的教程
评论
13条评论pony Lv.5
Chrome 80.0.3987.132
Windows 回复
江西省上饶市 移动
林羡 Lv.1
Chrome 65.0.3325.181
Windows 回复
,请上车的乘客系好安全带~[/blockquote]广东省惠州市 电信
arham Lv.1
Chrome 63.0.3239.132
Windows 回复
论失败:评论内容需包含中文
巴基斯坦
Scarecrowヽ Lv.1
Chrome 51.0.2704.106
Windows 回复
不错,来学习德国鬼子的文章了。
湖南省邵阳市 移动
゛自导自演 Lv.1
Chrome 50.0.2661.94
Windows 回复
为什么只会首页压缩呢,文章页不会压缩
河南省 移动
Finally
Chrome 45.0.2454.101
Windows 7 x64 Edition 回复
回复了゛自导自演:可以的,QQ私聊!
湖南省怀化市 联通