侧边栏壁纸
    • 累计撰写 208 篇文章
    • 累计收到 576 条评论
    Emlog无需插件实现网站源代码压缩
    2022-08-07  31 阅读 0 评论 1 点赞

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

    奉天
    2022-08-07 / 0 评论 / 31 阅读 / 检测收录中......
    温馨提示:
    本文最后更新于2022年08月07日,已超过627天没有更新,若内容或图片失效,请留言反馈。
    广告

    介绍

    网站源代码压缩的用处:
    1、减小了文件的体积。
    2、减小了网络传输量和带宽占用。
    3、减小了服务器的处理的压力。
    4、提高了页面的渲染显示的速度。

    代码展示

    先把下面的代码放到 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("nn", "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.phppage.php 文件中的 $log_content 用下面代码替换。

    slbys($log_content)

    最后大功告成,访问 view-source:https://域名/ 就能看到源码压缩后的代码了。

    1

    海报

    正在生成.....

    评论 (0)

    取消