Skip to content

Instantly share code, notes, and snippets.

@miqueiaspenha
Forked from willmendesneto/htmlMinification.php
Created September 23, 2020 12:42
Show Gist options
  • Save miqueiaspenha/a638c96f7434fba3def3feba2a581af7 to your computer and use it in GitHub Desktop.
Save miqueiaspenha/a638c96f7434fba3def3feba2a581af7 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