Skip to content

Instantly share code, notes, and snippets.

@willmendesneto
Created October 30, 2013 17:09
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save willmendesneto/7236329 to your computer and use it in GitHub Desktop.
Save willmendesneto/7236329 to your computer and use it in GitHub Desktop.
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