Optimize Your HTML
July 7th, 2008
Usually, your web page contains new lines (”\n” or “\r”), or tabs. In most cases, you don’t need those new lines or tabs (except there is a “<pre>” somewhere in your page).
If you removed those characters, your page size will be decreased dramatically (in most cases - again). So what should you do? Use the PHP code below. When a page is displayed to user’s browser, those characters will be removed (only when it is displayed too the browser :)).
<?php
ob_start('ob_callback');
function ob_callback($buffer) {
$buffer = str_replace("\n", '', $buffer);
$buffer = str_replace("\r", '', $buffer);
$buffer = str_replace("\t", '', $buffer);
$buffer = preg_replace("/<!\-\-([a-z0-9 \-\:\(\)\=\"\<\>\.\,\/]+)?\-\->/i", '', $buffer);
return trim($buffer);
}
?>
<html>
<!-- Put any content here -->
</html>
<?php ob_end_flush(); ?>
The secret is in the “ob_start()“. You can use it to your JavaScript or your CSS files too (with a little bit of modification - of course).


Recent Comments