在以往的Emlog优化教程中,相信给站长朋友们提供的都是使用的代码压缩插件
今天冬镜博客主要是分享插件的代码版本,即不使用插件直接将代码写进module.php中就可以
偷偷笑笑又成功的消灭一个插件
网站源代码压缩代码一:
首先我们找到模版文件夹下的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文件并打开 添加如下代码
<?php
if(_g('compress_html')=='open'){
$html=ob_get_contents();
ob_get_clean();
echo em_compress_html_main($html);
}
?>
网站源代码压缩代码二:
首先我们找到模版文件夹下的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);?>
Emlog无插件实现网站源代码压缩
如果你想要实现不压缩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)