如何在不删除IE条件注释的情况下缩小php html输出


How to minify php html output without removing IE conditional comments?

我用这个PHP脚本缩小了我的HTML页面:

function compress_html($html)
{
    preg_match_all('!(<(?:code|pre|script).*>[^<]+</(?:code|pre|script)>)!', $html, $pre);
    $html = preg_replace('!<(?:code|pre).*>[^<]+</(?:code|pre)>!', '#pre#', $html);
    $html = preg_replace('#<!–[^'[].+–>#', '', $html);
    $html = preg_replace('/['r'n't]+/', ' ', $html);
    $html = preg_replace('/>['s]+</', '><', $html);
    $html = preg_replace('/'s+/', ' ', $html);
    if (!empty($pre[0])) {
        foreach ($pre[0] as $tag) {
        $html = preg_replace('!#pre#!', $tag, $html,1);
        }
    }
    return $html;
}
ob_start('compress_html');

有一种方法可以只删除"HTML 注释"......而不是IE条件注释?

谢谢。

您的代码不处理多行 HTML 注释,只处理第一行。此外,如果您/您的服务器使用 gzip 压缩,您从中获得的节省可以忽略不计。即:

Uncompressed, un-minified page: 2209 bytes
Compressed,   un-minified page:  959 bytes
Uncompressed, minified page:    1973 bytes
Compressed,   minified page:     914 bytes

最后两点:

  1. 缩小您的 HTML 使其几乎无法阅读,因此祝您好运解决任何问题。
  2. 较大的页面通常具有更好的压缩率,尤其是在数据重复的情况下。