Created
March 23, 2017 13:05
-
-
Save BlaM/8a11e4bda8d3cd8c829e706b4c61b9b4 to your computer and use it in GitHub Desktop.
Some old PHP code I use to minify HTML code.
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 | |
function ob_end_flush_minified() { | |
$content = ob_get_contents(); | |
ob_end_clean(); | |
$content = minify_html($content); | |
echo $content; | |
} | |
function minify_html_sub ($content) { | |
$content = preg_replace('/[\s]+/'," ", $content); | |
$content = preg_replace('~ (<(ul|/ul|ol|/ol|li|/li|div|/div|table|/table|td|/td|tr|/tr|th|/th|br|body|/body|html|/html|head|/head|h[123456]|/h[123456]|dl|/dl|dd|/dd|dt|/dt|form|/form|iframe|/iframe|hr|map|/map|area|/area|p|/p|pre|select|/select|option|/option|style)[^>]*>)~',"$1", $content); | |
$content = preg_replace('~(<(ul|/ul|ol|/ol|li|/li|div|/div|table|/table|td|/td|tr|/tr|th|/th|br|applet|body|/body|html|/html|head|/head|h[123456]|/h[123456]|dl|/dl|dd|/dd|dt|/dt|form|/form|iframe|/iframe|hr|map|/map|area|/area|p|/p|/pre|selecy|/select|option|/option|/style)[^>]*>) ~',"$1", $content); | |
$content = preg_replace('~(<script [^>]*><!--) *~',"$1\n", $content); | |
$content = preg_replace('~ *(//--></script>)~',"\n$1", $content); | |
return $content; | |
} | |
function minify_html ($buffer){ | |
$poz_current = 0; | |
$poz_end = strlen($buffer)-1; | |
$result = ""; | |
while ($poz_current < $poz_end){ | |
$end_tag = null; | |
$t_poz_textarea_start = stripos($buffer, "<textarea", $poz_current); | |
$t_poz_pre_start = stripos($buffer, "<pre", $poz_current); | |
$t_poz_start = false; | |
if (($t_poz_textarea_start !== false && $t_poz_pre_start === false) || ($t_poz_textarea_start !== false && $t_poz_pre_start !== false && $t_poz_textarea_start < $t_poz_pre_start)) { | |
$t_poz_start = $t_poz_textarea_start; | |
$t_poz_end = stripos($buffer, "</textarea>", $t_poz_start); | |
if ($t_poz_end === false) $t_poz_start = false; | |
} | |
if (($t_poz_pre_start !== false && $t_poz_textarea_start === false) || ($t_poz_pre_start !== false && $t_poz_textarea_start !== false && $t_poz_pre_start < $t_poz_textarea_start)) { | |
$t_poz_start = $t_poz_pre_start; | |
$t_poz_end = stripos($buffer, "</pre>", $t_poz_start); | |
if ($t_poz_end === false) $t_poz_start = false; | |
} | |
if ($t_poz_start !== false) { | |
$buffer_part_2strip = substr($buffer, $poz_current, $t_poz_start-$poz_current); | |
$temp = minify_html_sub($buffer_part_2strip); | |
$result .= $temp; | |
$temp = substr($buffer, $t_poz_start, $t_poz_end-$t_poz_start); | |
$result .= $temp; | |
$poz_current = $t_poz_end; | |
} else { | |
$buffer_part_2strip = substr($buffer, $poz_current); | |
$temp = minify_html_sub($buffer_part_2strip); | |
$result .= $temp; | |
$poz_current = $poz_end; | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment