-
-
Save miqueiaspenha/a638c96f7434fba3def3feba2a581af7 to your computer and use it in GitHub Desktop.
Simple HTML Output minifcation example
This file contains 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 | |
/** | |
* Minify HTML output (HTML) | |
* | |
* How to use: | |
* | |
* Call function using ob_start(); | |
* | |
* if(extension_loaded('zlib') && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE){ | |
* ob_start('minify_output'); | |
* } | |
* @param type $output | |
* @return type | |
*/ | |
function minify_output($output){ | |
ob_gzhandler(); | |
// Clean comments | |
// Remove comments (non-MSIE conditionals) | |
// Remove CSS comments | |
// Clean Whitespace | |
$regex_expression = array('{\s*<!--[^\[<>].*(?<!!)-->\s*}msU', '!/\*[^*]*\*+([^/][^*]*\*+)*/!i', '/<!--(.|s)*?-->/', '/\s{2,}/', '/(\r?\n)/'); | |
$arrayOutput = array('', '', '', '', ''); | |
$output = preg_replace($regex_expression, $arrayOutput, $output); | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment