Compressing your HTML page using PHP
Compressing a HTML page in php in very simple to implement especially if you use a content management system for your website.
PHP CODE:
Compress HTML
- <?php
- // Start output buffer.
- // html code
- ?>
- <!-- place all your html code here -->
- <?php
- // function to compress page.
- function compress_page($buffer) {
- // remove comments, tabs, spaces, newlines, etc.
- "/ +/" => " ",
- "/<!--(.*?)-->|[\t\r\n]|<!--|-->|\/\/ <!--|\/\/ -->|<!\[CDATA\[|\/\/ \]\]>|\]\]>|\/\/\]\]>|\/\/<!\[CDATA\[/" => ""
- );
- return $buffer;
- }
- // turn off output buffering and output the page.
- ?>
And that's it at it's very basic!
This function will not compress commented JavaScript, it will compress the JavaScript that's not commented. For JavaScript compression it is best to serve the JavaScript from external files and compressed with either JavaScriptPacker.php or YUI compressor. CSS compression can be further done with another compression function and I will show this in another tutorial.
Main Menu
Ubuntu
Donate
If you find this website useful and you would like to contribute feel free to by using the button below.
Site Specs
Site best viewed in a screen resolution of 1024x768 & 32 bit colours or higher using the latest version of Firefox
