I minify my HTML pages whit this PHP script:
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('/[
\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?
Thanks.
Your code doesn't handle multi-line HTML comments, only removing the first line. Also, the savings you get from this are negligible if you/your server is using gzip compression. 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: