'; function __minifyx($input) { return str_replace(array("\n", "\t", ' '), array(X . '\n', X . '\t', X . '\s'), $input); } function __minifyv($input) { return str_replace(array(X . '\n', X . '\t', X . '\s'), array("\n", "\t", ' '), $input); } /** * ======================================================= * HTML MINIFIER * ======================================================= */ function __replace_html_style_for_minify($m) { return $m[1] . $m[2] . minify_css($m[3]) . $m[2]; } function __replace_html_for_minify($m) { if(isset($m[2])) { // Minify inline CSS declaration(s) if(stripos($m[2], ' style=') !== false) { $m[2] = preg_replace_callback('#( style=)([\'"]?)(.*?)\2#i', '__replace_html_style_for_minify', $m[2]); } return '<' . $m[1] . preg_replace( array( // From `defer="defer"`, `defer='defer'`, `defer="true"`, `defer='true'`, `defer=""` and `defer=''` to `defer` [^1] '#\s(checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped)(?:=([\'"]?)(?:true|\1)?\2)#i', // Remove extra white-space(s) between HTML attribute(s) [^2] '#\s*([^\s=]+?)(=(?:\S+|([\'"]?).*?\3)|$)#', // From `` to `` [^3] '#\s+\/$#' ), array( // [^1] ' $1', // [^2] ' $1$2', // [^3] '/' ), str_replace("\n", ' ', $m[2])) . '>'; } return '<' . $m[1] . '>'; } function _minify_html($input) { return preg_replace_callback('#<\s*([^\/\s]+)\s*(?:>|(\s[^<>]+?)\s*>)#', '__replace_html_for_minify', $input); } function minify_html($input) { if( ! $input = trim($input)) return $input; global $CH; // Keep important white-space(s) after self-closing HTML tag(s) $input = preg_replace('#(<(?:img|input)(?:\s[^<>]*?)?\s*\/?>)\s+#i', '$1' . X . '\s', $input); // Create chunk(s) of HTML tag(s), ignored HTML group(s), HTML comment(s) and text $input = preg_split('#(' . $CH . '||\s[^<>]*?>)[\s\S]*?<\/pre>||\s[^<>]*?>)[\s\S]*?<\/code>||\s[^<>]*?>)[\s\S]*?<\/script>||\s[^<>]*?>)[\s\S]*?<\/style>||\s[^<>]*?>)[\s\S]*?<\/textarea>|<[^<>]+?>)#i', $input, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE); $output = ""; foreach($input as $v) { if($v !== ' ' && trim($v) === "") continue; if($v[0] === '<' && substr($v, -1) === '>') { if($v[1] === '!' && strpos($v, '') continue; $output .= $v; } else { $output .= __minifyx(_minify_html($v)); } } else { // Force line-break with ` ` or ` ` $v = str_replace(array(' ', ' ', ' '), X . '\n', $v); // Force white-space with ` ` or ` ` $v = str_replace(array(' ', ' '), X . '\s', $v); // Replace multiple white-space(s) with a space $output .= preg_replace('#\s+#', ' ', $v); } } // Clean up ... $output = preg_replace( array( // Remove two or more white-space(s) between tag [^1] '#>([\n\r\t]\s*|\s{2,})<#', // Remove white-space(s) before tag-close [^2] '#\s+(<\/[^\s]+?>)#' ), array( // [^1] '><', // [^2] '$1' ), $output); $output = __minifyv($output); // Remove white-space(s) after ignored tag-open and before ignored tag-close (except `