近期有不少的站长朋友来问我,Emlog程序搭建的网站压缩代码是不是对优化有好处?今天,冬镜就和大家聊聊网站代码压缩的好处以及Emlog网站无需插件即可实现网站代码压缩。

emlog教程
一、网站代码压缩对优化有什么好处?
1、什么是网站代码
网站代码就是网页的代码,一般可以用个 Ctrl+U 查看网页源代码。做为一名合格的SEO人员, 必须要学会看得懂代码。
2、什么是网站优化
网站优化简单一点说就是搜索引擎优化,在了解搜索引擎的规则的基础上,进行站内站外的优化。网站优化的目的就是提高网站收录,达成最后的转化成交。
所以压缩网站代码站在优化的角度是可以提高搜索用户打开网页的速度,从另一方面来说符合百度的闪电算法,可以更好的提高网站的收录。

网站优化
二、Emlog网站代码压缩教程
1、方法一
找到模版文件下的 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); } ?>
2、方法二
打开模版文件下的 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)
评论0