Created
August 23, 2018 11:18
-
-
Save vpadhariya/e3c43ab17d770cb0cf56b53e38c5202a to your computer and use it in GitHub Desktop.
Sanitize output of html, remove html, css, js comments and trim output.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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