Compress your HTML code using PHP

Sharing is caring!

When we write code we love to format it nicely adding tabs, line breaks and indentations, but the end user isn’t interested in how lovely the source code is, they just want the page content, so this script strips out all the line breaks and spaces in your code and puts it on one line, compressing your code and making it faster.

<?php
// start output buffer
ob_start(‘compress_html’);
?>

<!– all xhtml content here –>

<?php
// end output buffer and echo the page content
ob_end_flush();

// this function gets rid of tabs, line breaks, and white space
function compress_html($compress)
{
$i = array(‘/>[^S ]+/s’,’/[^S ]+</s’,’/(s)+/s’);
$ii = array(‘>’,'<‘,’1’);
return preg_replace($i, $ii, $compress);
}
?>

2 thoughts on “Compress your HTML code using PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Got Project on mind? Let's ConnectContact Us

Secured By miniOrange