SHARE

In this article i write the easy way to compress html delivery on website. HTML compression is a very nessery thing to speed up a website and reduce loading time. Indirectoly Html compression is also a part of search engine optimization. Because when web developer create a website many of white spaces and comments and written obviously  for neat and clean coding. for example:

 

Html Before Compression-Tectrick.orgHtml Before Compression-Tectrick.org
Html Before Compression-Tectrick.org

In the upper example code tag <html> to </html> looks very clean and it is best during coding.

But all of white spaces between <html> to </html> is occupy space on server. So it means more blank spaces means more loading time of website. There are to ways to remove these useless white spaces .One is remove it manually from html code or second is remove it at the time of html delivery by any function ,module or plugin.First method ,remove it manually from html code is not a genuine method ,because if  we clean white space and comments from code from backend than at next time modification of coding is very difficult so that code is looks like very complex and not easy to read

<html><head></head><body>——website content here—-</body></html>

and modification is very difficult. So use second method.Copy the following code started from “function sanitize” to “output” );” . and paste it anywere in between <head> tag in html code.If  website is created by any script like wordpress or prestashop than place the code in header.php  file in just after <head> tag  or paste this code in end of  wp-config.php  in wordpress.

Code to compress HTML

function sanitize_output($buffer)
{ $search = array( '/\>[^\S ]+/s', // strip whitespaces after tags, except space '/[^\S ]+\', '< ', '\\1' ); $buffer = preg_replace($search, $replace, $buffer); return $buffer;
} ob_start("sanitize_output");

After paste the code save the file. Reload load your website. you will see loading time of website is reduced. To see ,code is working ,right click on webpage and click on view page source tab. compress html is visible .This code is also work on php websites

Html after Compression-Tectrick.orgl
Html after Compression-Tectrick.orgl

for more help ask question on forum on html and css.