Created
December 1, 2019 20:32
-
-
Save iamrobert/5aeaeb79dbe45cc778682b5fc19300b9 to your computer and use it in GitHub Desktop.
minimize html in template (not working)
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 sanitize_output($buffer) { | |
| $x = '<!--(?>-?[^-]*+)*?--!?>'; | |
| $s1 = DOUBLE_QUOTE_STRING; | |
| $s2 = SINGLE_QUOTE_STRING; | |
| //Regex for escape elements | |
| $pr = "<pre\b[^>]*+>(?><?[^<]*+)*?</pre\s*+>"; | |
| $sc = "<script\b[^>]*+>(?><?[^<]*+)*?</script\s*+>"; | |
| $st = "<style\b[^>]*+>(?><?[^<]*+)*?</style\s*+>"; | |
| $tx = "<textarea\b[^>]*+>(?><?[^<]*+)*?</textarea\s*+>"; | |
| $rx = "#(?><?[^<]*+(?>$pr|$sc|$st|$tx|<!--\[(?><?[^<]*+)*?" | |
| . "<!\s*\[(?>-?[^-]*+)*?--!?>|<!DOCTYPE[^>]++>)?)*?\K(?:$x|$)#i"; | |
| $rx2 = "#(?>[^<]*+(?:$pr|$sc|$st|$tx|$x|<(?>[^>=]*+(?:=\s*+(?:$s1|$s2|['\"])?|(?=>)))*?>)?)*?\K" | |
| . '(?:[\t\f ]++(?=[\r\n]\s*+<)|(?>\r?\n|\r)\K\s++(?=<)|[\t\f]++(?=[ ]\s*+<)|[\t\f]\K\s*+(?=<)|[ ]\K\s*+(?=<)|$)#i'; | |
| // remove ws around block elements preserving space around inline elements | |
| //block/undisplayed elements | |
| $b = 'address|article|aside|audio|body|blockquote|canvas|dd|div|dl' | |
| . '|fieldset|figcaption|figure|footer|form|h[1-6]|head|header|hgroup|html|noscript|ol|output|p' | |
| . '|pre|section|style|table|title|tfoot|ul|video'; | |
| //self closing block/undisplayed elements | |
| $b2 = 'base|meta|link|hr'; | |
| //inline elements | |
| $i = 'b|big|i|small|tt' | |
| . '|abbr|acronym|cite|code|dfn|em|kbd|strong|samp|var' | |
| . '|a|bdo|br|map|object|q|script|span|sub|sup' | |
| . '|button|label|select|textarea'; | |
| //self closing inline elements | |
| $i2 = 'img|input'; | |
| $rx3 = "#(?>\s*+(?:$pr|$sc|$st|$tx|$x|<(?:(?>$i)\b[^>]*+>|(?:/(?>$i)\b>|(?>$i2)\b[^>]*+>)\s*+)|<[^>]*+>)|[^<]++)*?\K" | |
| . "(?:\s++(?=<(?>$b|$b2)\b)|(?:</(?>$b)\b>|<(?>$b2)\b[^>]*+>)\K\s++(?!<(?>$i|$i2)\b)|$)#i"; | |
| $j = '<input type="hidden" name="[0-9a-f]{32}" value="1" />'; | |
| //Remove additional ws around attributes | |
| $rx5 = "#(?>\s?(?>[^<>]*+(?:<!(?!DOCTYPE)(?>>?[^>]*+)*?>[^<>]*+)?<|(?=[^<>]++>)[^\s>'\"]++(?>$s1|$s2)?|[^<]*+))*?\K" | |
| . "(?>\s\s++|$)#i"; | |
| //Remove last whitespace in open tag | |
| $rx6 = "#(?>[^<]*+(?:$j|$x|<(?![a-z0-9]++))?)*?(?:<[a-z0-9]++(?>\s*+[^\s>]++)*?\K" | |
| . "(?:\s*+(?=>)|(?<=[\"'])\s++(?=/>))|$\K)#i"; | |
| $search = array( | |
| $rx, | |
| $rx2, | |
| $rx3, | |
| $rx5, | |
| $rx6 | |
| ); | |
| $replace = array( | |
| '', | |
| '', | |
| '', | |
| ' ', | |
| '' | |
| ); | |
| $buffer = preg_replace($search, $replace, $buffer); | |
| return $buffer; | |
| } | |
| // Usage | |
| ob_start('sanitize_output'); | |
| ?> | |
| <html>.............. | |
| <?php | |
| ob_end_flush(); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment