Code

Coding, Programming & Algorithms, Tips, Tweaks & Hacks
Search

Compressing HTML

Compress might not be the right word, as it doesn't really compress / zip the output HTML code, but combines all the HTML lines to a single line and removes trailing / leading whitespaces.

This has to be your index.php and am assuming you are routing all incoming page requests to index.php which will be handled by either rewrites in htaccess or direct query strings. For example, I would have either

RewriteRule ^about\/vision$ index.html?page=about-vision [QSA,L]
or
RewriteRule ^register$ index.php?module=Register

PHP
<?php
ob_start();
require_once "index.phtml";
$html = ob_get_clean();
$htmls = explode("\n", $html);
for ($i = 0; $i < count($htmls); $i++)
{
    $htmls[$i] = trim($htmls[$i]);
}
$html = implode("", $htmls);
echo $html;
?>
PHP 5

There is one gotcha in this. You can't use single line comments start with // in inline Javascript code in the HTML. Either place the JavaScript code outside of the HTML page, in separate .js page or use multi-line comments /* */

Vanakkam !

0 comments: