Compressed HTML ouput using PHP

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);
}
?>
Farhamdani

Sharing insights on tech, blogging, and passive income. Follow for more at farhamdani.eu.org!

Drop your comments, but make sure they’re related to the discussion!

I'd be grateful if you could read the Commenting Rules on this blog before posting a comment.

Post a Comment (0)
Previous Post Next Post