首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Emlog无插件实现网站源码压缩

Emlog无插件实现网站源码压缩

作者头像
用户8099761
发布于 2023-05-10 20:24:43
发布于 2023-05-10 20:24:43
7670
举报
文章被收录于专栏:私人订制私人订制

在以往的Emlog优化教程中,相信都是使用的代码压缩插件,今天主要是分享插件的代码版本,也就是不使用插件,直接将代码丢在module.php中就可以,好吧,又消灭一个插件!

 以下代码是扔在module.php里面的

代码语言:javascript
复制
<?php //全站代码压缩
function slys($sheli){$initial=strlen($sheli);$sheli=explode("<!--slys-->",$sheli);$count=count($sheli); for($i=0;$i<=$count;$i++){if(stristr($sheli[$i], '<!--slys end-->')){$sheli[$i]=(str_replace("<!--slys end-->", " ", $sheli[$i]));}else{$sheli[$i]=(str_replace("\t", " ", $sheli[$i]));$sheli[$i]=(str_replace("\n\n", "\n", $sheli[$i]));$sheli[$i]=(str_replace("\n", "", $sheli[$i]));$sheli[$i]=(str_replace("\r", "", $sheli[$i]));while (stristr($sheli[$i], '  ')){$sheli[$i]=(str_replace("  ", " ", $sheli[$i]));}}$sheli_out.=$sheli[$i];}$final=strlen($sheli_out);$savings=($initial-$final)/$initial*100;$savings=round($savings,2);$sheli_out.="\n<!--压缩前的大小: $initial bytes; 压缩后的大小: $final bytes; 节约:$savings% -->";return $sheli_out;}
?>

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

代码语言:javascript
复制
<?php $html=ob_get_contents();ob_get_clean();echo slys($html);?>

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

代码语言:javascript
复制
<?php //不压缩pre
function slbys($content){if(preg_match_all('/(crayon-|<\/pre>)/i',$content,$matches)){$content = '<!--slys--><!--slys end-->'.$content;$content.= '<!--slys end--><!--slys-->';}return $content;}slbys($log_content);?>

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

代码语言:javascript
复制
slbys($log_content)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-9-30,如有侵权请联系 cloudcommunity@tencent.com 删除
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档