Web Tutorials     [RO]  [EN]  
| HOME | Tutorials | News | SERVICES | Directory | Tools | FORUM | About | SITE MAP | CONTACT | SEARCH |
.....................................................
.....................................................
User happy birthdayToday we celebrate one day of birth.
(dannyb0y)
.....................................................
Login
Register
I forgot my password
.....................................................
Put your ad here
.....................................................
Online
In total there is
9 visitors online,
of which:
2 we have
7 are bots
.....................................................
Put your ad here
.....................................................
.....................................................
.....................................................
.....................................................
.
Home - Minifying and gzipping HTML on the run with PHP

<< PHP MVC   -   What is PayPal? How do we use it? >>
Rate this article(Members only)
1 2 3 4 5
A - A Announced this way the site administrator for any problems observed on this page.  Print this page as PDF  Email  

Minifying and gzipping HTML on the run with PHP


Publishing date: 04-03-2011 - Copyright © Claudiu Gilcescu-Ceia

Continuing on the same topic as before, when I wrote about CSS minifying and compression, a major improvement can be done on HTML as well, but it's a bit trickier. 

Simply removing white spaces from an HTML document can lead to problems in the way the document itself is displayed, validation errors and tons of other problems. Still, HTML gzip compression should take out most of the whitespace, so we could maybe just consider stripping comments. Still, make sure that there's no bit of code, Javascript especially, that relies on some HTML comments, as that will obviously break them. 

This is very easily done with PHP, using the output buffering methods used in the CSS example demo. No matter if you use a framework or a "hand-made" system, you most likely have a file that you include at the very top of every page and one that you include at the bottom. Anyway, just before you output anything to the browser, make sure you add these lines: 

CODE:
ob_start("ob_gzhandler"); // Start the gzip output buffer
ob_start("minify_html"); // Custom callback in this nested call
function minify_html($buffer) {
   return preg_replace('/<!--(.*)-->/Uis', '', $buffer); // Strip anything found between
}

Altough the output buffers will eventually close themselves at the end of the script, it's wiser to close them yourself at the end of the page. 

CODE:
ob_end_flush();
ob_end_flush();

Pay attention, as the function to strip comments might not offer the desired result in some cases. In some sittuations, you might actually have some data you need between those tags. Doing anything other than that in an automatic fashion will end up at some point, as HTML mark-up can be quite tricky and the best way is to do all the "dirty work" manually, rather than automatically. Or, if you have static pages, minify those using Google's Page Speed utility, make sure they look ok and are functional, and then use those. 

Running this on this blog was a great result. I could get around 2.4kb more if I minify the pages using the algorithm that Page Speed does, but then again, that strips out <head/> tags and such as they're not mandatory and take up space, so it's a bit drastic I think.

The code in this article is distributed under the MIT license licence

You can visit the author's website at blog.hazardousgaming.info

Publishing date: 04-03-2011 - Copyright © Claudiu Gilcescu-Ceia   
Click here if you want to see other articles by the same author
There are no comments on this article. Be the first to say your opinion.

Add a comment on this article (members only - login on the site):
Put your ad here