Created
November 3, 2011 23:46
-
-
Save timw4mail/1338288 to your computer and use it in GitHub Desktop.
HTML Minification
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 | |
define('SAFE', 1); | |
define('EXTREME', 2); | |
define('EXTREME_SAVE_COMMENTS', 4); | |
define('EXTREME_SAVE_PRE', 3); | |
function minify($html, $level=2) | |
{ | |
switch((int)$level) | |
{ | |
case 0: | |
//Don't minify | |
break; | |
case 1: //Safe Minify | |
// Replace all whitespace characters between tags with a single space | |
$html = preg_replace("`>\s+<`", "> <", $html); | |
break; | |
case 2: //Extreme Minify | |
// Placeholder to save conditional comment hack, <pre> and <code> tags | |
$place_holders = array( | |
'<!-->' => '_!--_', | |
); | |
//Set placeholders | |
$html = strtr($html, $place_holders); | |
// Remove all normal comments - save conditionals | |
$html = preg_replace('/<!--[^(\[|(<!))](.*)-->/Uis', '', $html); | |
// Replace all whitespace characters with a single space | |
$html = preg_replace("`\s+`", " ", $html); | |
// Remove the spaces between adjacent html tags | |
$html = preg_replace("`> <`", "><", $html); | |
// Replace space between adjacent a tags for readability | |
$html = str_replace("</a><a", "</a> <a", $html); | |
// Restore placeholders | |
$html = strtr($html, array_flip($place_holders)); | |
break; | |
case 3: //Extreme, save pre and code tags | |
// Placeholder to save conditional comment hack, <pre> and <code> tags | |
$place_holders = array( | |
'<!-->' => '_!--_', | |
'<pre>' => '_pre_', | |
'</pre>' => '_/pre_', | |
'<code>' => '_code_', | |
'</code>' => '_/code_' | |
); | |
//Set placeholders | |
$html = strtr($html, $place_holders); | |
// Remove all normal comments - save conditionals | |
$html = preg_replace('/<!--[^(\[|(<!))](.*)-->/Uis', '', $html); | |
// Replace all whitespace characters with a single space | |
$html = preg_replace(">`\s+`<", "> <", $html); | |
// Remove the spaces between adjacent html tags | |
$html = preg_replace("`> <`", "><", $html); | |
// Replace space between adjacent a tags for readability | |
$html = str_replace("</a><a", "</a> <a", $html); | |
// Restore placeholders | |
$html = strtr($html, array_flip($place_holders)); | |
break; | |
case 4: //Extreme minify, save comments | |
// Replace all whitespace characters with a single space | |
$html = preg_replace("`\s+`", " ", $html); | |
// Remove spaces between adjacent html tags | |
$html = preg_replace("`> <`", "><", $html); | |
// Restore space between ajacent a tags | |
$html = str_replace("</a><a", "</a> <a", $html); | |
break; | |
} | |
//Normalize ampersands | |
$html = str_replace("&", "&", $html); | |
$html = str_replace("&", "&", $html); | |
//Replace common entities with more compatible versions | |
$replace = array( | |
' ' => ' ', | |
'©' => '©', | |
'â' => 'â', | |
'¢' => '¢', | |
'»' => '»', | |
'«' => '«' | |
); | |
$html = strtr($html, $replace); | |
//Return minified html | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment