Created
January 19, 2022 17:17
-
-
Save Vitzkrieg/4b40e85625050ef864c98c9a7c2f2d9f to your computer and use it in GitHub Desktop.
This file contains 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 | |
add_action('get_header', 'pt_html_minify_start'); | |
function pt_html_minify_start() { | |
ob_start( 'pt_html_minyfy_finish' ); | |
} | |
function pt_html_minyfy_finish( $html ) { | |
$html = preg_replace('/<!--(?!s*(?:[if [^]]+]|!|>))(?:(?!-->).)*-->/s', '', $html); | |
$html = preg_replace('/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/m', "$1", $html); | |
$html = preg_replace('/^\/\/[^\n\r]+(?:[\n\r]|\*\))$/m', "$1", $html); | |
$html = str_replace(array("\r\n", "\r", "\n", "\t"), '', $html); | |
while ( stristr($html, ' ')) | |
$html = str_replace(' ', ' ', $html); | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment