Create a gist now

Instantly share code, notes, and snippets.

Simple HTML Output minifcation example
<?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