您可以做的最好的事情之一是删除不需要内联的任何内联脚本。
我有这个在我的PHP文件的顶部:
ob_start();
ob_implicit_flush(0);
然后,我有这个功能:(我的笔记不说我来自哪里偷来的)
function print_gzipped_page() {
$HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
if(headers_sent())
$encoding = false;
else if(strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false)
$encoding = 'x-gzip';
// *** I can't recall why I disabled this one ***
// I had some device that it didn't work with.
//else if(strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false)
// $encoding = 'gzip';
else {
$encoding = false;
}
$contents = ob_get_clean();
if ($encoding)
{
$_temp1 = strlen($contents);
if ($_temp1 < 2048) { // no need to waste resources in compressing very little data
print($contents);
} else {
header('Content-Encoding: '.$encoding);
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$contents = gzcompress($contents, 9);
$contents = substr($contents, 0, $_temp1);
print($contents);
}
}
else {
print($contents);
}
}
在在页面的最下方,我只要致电:
print_gzipped_page();
正如代码说,如果你已经发送了头 - 如果你已经发送任何输出,基本上,那么这个代码不是为你压缩任何东西。
看看[这里](http://stackoverflow.com/questions/6225351/how-to-minify-php-page-html-output)。第一个答案全部变成了 – karka91