压缩HTML代码 CompressHTML For Typecho

插件开发及发布

版主: woniou

lonewolf
帖子: 1
注册时间: 2017年 6月 10日 18:16

Re: 压缩HTML代码 CompressHTML For Typecho

帖子 lonewolf »

感觉不需要使用插件,我个人是不怎么喜欢使用插件的,大部分我都会集合在模板里面。

functions.php:
//网站源码压缩函数
function tp_compress_html_main($buffer){
$initial=strlen($buffer);
$buffer=explode("<!--tp-compress-html-->", $buffer);
$count=count ($buffer);
for ($i = 0; $i <= $count; $i++){
if (stristr($buffer[$i], '<!--tp-compress-html no compression-->')){
$buffer[$i]=(str_replace("<!--tp-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);
return $buffer_out;
}

//内容页面禁止PRE压缩
function unCompress($content){
if(preg_match_all('/(crayon-|<\/pre>)/i', $content, $matches)) {
$content = '<!--tp-compress-html--><!--tp-compress-html no compression-->'.$content;
$content.= '<!--tp-compress-html no compression--><!--tp-compress-html-->';
}
return $content;
}


footer.php:
$html=ob_get_contents();
ob_get_clean();
echo tp_compress_html_main($html);
回复