How to remove Whitespaces and comments in html output and decrease pageload time with php

Published


Here is a simple method for you to decrease your page size and increase your page load time with php.
It is only supported if you have a host where php is supported.



Have you ever noticed that when we view source code of any website, or blog, (Unluckily there are no client side work we can do about that in blogger), we can easily find and read html tags because they are written same like a script that we can differentiate between each tags and variables from that, because after each element there will be whitespaces till next
just like this

Imagine this is what we have in our index.php file - When we open it in web browser and view source code, it will remain just like this.

<html>
<head>
<title>Aslamise<title/>
</head>
<body>
Here goes the contents
<br/>
<a href='http://aslamise.blogspot.com'>content here</a>
<!--this is a comment-->
</body>
<html>
So, when the whitespaces are removed in our output,source code will be like this. and original file will be same like above.
<html><head><title>Aslamise<title/></head><body>Here goes the contents<br/><a href='http://aslamise.blogspot.com'>content here</a> </body><html>
I guess you have understood what are whitespaces and comments now.

So, in first html it was total length of 188 bytes .
After we removed whitespaces and comments, the length decreased to 144 !!

If you want to remove whitespaces, and comments in your html page that you are hosted in a php supported host, then you can do this thing to decrease your page load time by decreasing unwanted quotes and comments, white spaces etc.

Here is the script for removeing whitespaces and comments with php.


<?php function callback($buffer){     
$buffer = str_replace("\n", "", $buffer);  
$buffer = str_replace("//", "//", $buffer);     
$buffer = str_replace("\t", "", $buffer);     
$buffer = str_replace(chr(13), "", $buffer);     
$buffer = ereg_replace("<!\-\- [\/\ a-zA-Z]* \-\->", "", $buffer);  
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); // remove comments     
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer); // remove tabs, spaces, newlines, etc.     
$buffer = str_replace('{ ', '{', $buffer); // remove unnecessary spaces.     $buffer = str_replace(' }', '}', $buffer);     
$buffer = str_replace('; ', ';', $buffer);     
$buffer = str_replace(', ', ',', $buffer);     
$buffer = str_replace(' {', '{', $buffer);     
$buffer = str_replace('} ', '}', $buffer);     
$buffer = str_replace(': ', ':', $buffer);     
$buffer = str_replace(' ,', ',', $buffer);     
$buffer = str_replace(' ;', ';', $buffer);     
return $buffer; } 
ob_start("callback");
 ?> <a href='http://aslamise.blogspot.com'>
content here
</a> 
<?php ob_end_flush(); ?>

Ok, lets add it to our index.php

<?php function callback($buffer){     
$buffer = str_replace("\n", "", $buffer);  
$buffer = str_replace("//", "//", $buffer);     
$buffer = str_replace("\t", "", $buffer);     
$buffer = str_replace(chr(13), "", $buffer);     
$buffer = ereg_replace("<!\-\- [\/\ a-zA-Z]* \-\->", "", $buffer);  
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); // remove comments     
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer); // remove tabs, spaces, newlines, etc.     
$buffer = str_replace('{ ', '{', $buffer); // remove unnecessary spaces.     $buffer = str_replace(' }', '}', $buffer);     
$buffer = str_replace('; ', ';', $buffer);     
$buffer = str_replace(', ', ',', $buffer);     
$buffer = str_replace(' {', '{', $buffer);     
$buffer = str_replace('} ', '}', $buffer);     
$buffer = str_replace(': ', ':', $buffer);     
$buffer = str_replace(' ,', ',', $buffer);     
$buffer = str_replace(' ;', ';', $buffer);     
return $buffer; } ob_start("callback"); 
?> <html> 
<head>
 <title>
Aslamise
<title/> 
</head> 
<body> 
Here goes the contents 
<a href='http://aslamise.blogspot.com'>
content here
</a> 
<!--this is a comment--> 
</body> 
<html> 
<?php ob_end_flush(); ?>

Output will be obfuscated and the source code will remain same.

Comments

Post a Comment