Minify html output with simple php function

It’s always a pleasure to have html tags tidy and attached to one another. especially if these html tags are yours in your website. To have this we can do with a simple php function. Place the code below at the top of header php page or index page.

<?php
function sanitize_output($buffer) {
$search = array(
'/\>[^\S ]+/s',
'/[^\S ]+\</s',
'/(\s)+/s',
'/<!--(.|\s)*?-->/'
);
$replace = array(
'>',
'<',
'\\1',
''
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
ob_start("sanitize_output");
?>

Leave a Reply

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