简体   繁体   English

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

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

I minify my HTML pages whit this PHP script: 我用这个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');

There is a way to remove just the "HTML comments"...and not the IE conditional comments? 有一种方法可以只删除“ HTML注释” ...而不删除IE条件注释?

Thanks. 谢谢。

Your code doesn't handle multi-line HTML comments, only removing the first line. 您的代码不处理多行HTML注释,仅删除第一行。 Also, the savings you get from this are negligible if you/your server is using gzip compression. 另外,如果您/您的服务器使用gzip压缩,则由此节省的费用可以忽略不计。 ie: 即:

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

Uncompressed, minified page:    1973 bytes
Compressed,   minified page:     914 bytes

Last two points: 最后两点:

  1. Minifying your HTML makes it near-impossible to read, so good luck troubleshooting anything. 缩小HTML使其几乎无法阅读,因此,祝您一切顺利。
  2. Larger pages generally have better compression ratios, particularly if the data is repetitive. 较大的页面通常具有更好的压缩率,尤其是在数据重复的情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM