Loading... # Emlog无插件实现网站源码压缩 大家都清楚,一个网站运行的插件越多,速度相应的都会降低,所以我在去插件化这条路上一去不返,天天搜索相关的文章和资料。今天给大家分享一个网站源码压缩的代码。 在以往的Emlog优化教程中,相信都是使用的代码压缩插件,今天主要是分享插件的代码版本,也就是不使用插件,直接将代码丢在module.php中就可以,好吧,又消灭一个插件! ## 以下代码是扔在module.php里面的 ``` <?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最末尾(即后面) ``` <?php $html=ob_get_contents();ob_get_clean();echo slys($html);?> ``` ## 如果要实现不压缩pre中的代码,要在module.php里面的加入下面代码 ``` <?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用下面代码替换 ``` slbys($log_content) ``` <hr class="content-copyright" style="margin-top:50px" /><blockquote class="content-copyright" style="font-style:normal"><p class="content-copyright">版权所有:<a href="https://hux6.com"><strong><font color="ff0000">纪录美好生活</font></strong></a></p><p class="content-copyright">本文链接:<a class="content-copyright" href="https://hux6.com/archives/38.html">https://hux6.com/archives/38.html</a></p><p class="content-copyright">本站文章采用 <strong><font color="ff0000">知识共享署名4.0 国际许可协议</font></strong> 进行许可,可转载文章请在转载时注明出处及本声明!</p></blockquote> 最后修改:2020 年 08 月 24 日 01 : 55 PM © 允许规范转载 赞赏 如果觉得我的文章对你有用,请随意赞赏 ×Close 赞赏作者 扫一扫支付