Skip to content

Instantly share code, notes, and snippets.

@vpadhariya
Created August 23, 2018 11:18
Show Gist options
  • Save vpadhariya/e3c43ab17d770cb0cf56b53e38c5202a to your computer and use it in GitHub Desktop.
Save vpadhariya/e3c43ab17d770cb0cf56b53e38c5202a to your computer and use it in GitHub Desktop.
Sanitize output of html, remove html, css, js comments and trim output.
<?php
/*
* Compressing the HTML Output
*/
function sanitize_output($buffer)
{
$search = array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
'/<!--(.|\s)*?-->/', // Remove HTML comments
'/\>\s+\</', // Remove Extra space between Tags
);
$replace = array(
'>',
'<',
'\\1',
'',
'><',
);
$buffer = preg_replace($search, $replace, $buffer);
return $buffer;
}
// Now put this statement before starting the output
ob_start("sanitize_output");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment