当前位置:网站首页 > 更多 > 站长帮 > 正文

[Emlog教程] Emlog非插件实现网站源码压缩

作者:CC下载站 日期:2018-10-09 07:08:37 浏览:1943 分类:站长帮

将以下代码放在模版的module.php中:

<?php
//全站代码压缩
function maple5($maple){
    $initial=strlen($maple);
    $maple=explode("<!--em-compress-html-->", $maple);
    $count=count ($maple);
for($i =0; $i <= $count; $i++){
if(stristr($maple[$i],'<!--em-compress-html no compression-->')){
            $maple[$i]=(str_replace("<!--em-compress-html no compression-->"," ", $maple[$i]));
}else{
            $maple[$i]=(str_replace("\t"," ", $maple[$i]));
            $maple[$i]=(str_replace("\n\n","\n", $maple[$i]));
            $maple[$i]=(str_replace("\n","", $maple[$i]));
            $maple[$i]=(str_replace("\r","", $maple[$i]));
while(stristr($maple[$i],'  '))
{
            $maple[$i]=(str_replace("  "," ", $maple[$i]));
}
}
        $maple_out.=$maple[$i];
}
    $final=strlen($maple_out);
    $savings=($initial-$final)/$initial*100;
    $savings=round($savings,2);
    $maple_out.="\n<!--压缩前的大小: $initial bytes; 压缩后的大小: $final bytes; 节约:$savings% -->";
return $maple_out;
}
?>

以下代码放在footer.php最末尾(即后面)

<?php $html=ob_get_contents();ob_get_clean();echo maple5($html);?>

如果要实现不压缩pre中的代码,要在module.php中加入下面代码:

<?php 
//不压缩pre
function maplepre($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;
}
maplepre($log_content);
?>

然后在echo_log.phppage.php文件中的$log_content用下面代码替换:

maplepre($log_content)


您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯