Skip to content

Instantly share code, notes, and snippets.

@ivanhub
Created March 18, 2023 15:18
Show Gist options
  • Save ivanhub/c90e09ab39bd9c8b6dd994521fd9e96c to your computer and use it in GitHub Desktop.
Save ivanhub/c90e09ab39bd9c8b6dd994521fd9e96c to your computer and use it in GitHub Desktop.
best_html_minifier
<?php
if (!isset($_GET['dev'])) {
$cid = $modx->resource->get('id');
//$exclude = $modx->getOption('exclude',$scriptProperties,'12');
if(empty($cid)) { return; }
if(in_array($cid,explode(',',$exclude))) { return; }
$output = &$modx->resource->_output;
//remove all comments
// $output = preg_replace('/' . '(?:\/\*(?:[\s\S]*?)\*\/)|(?:<!--(?:[^\[][\s\S]*?)-->)|(?:([\s])+\/\/(?:.*))' . '/', '', $output);
// $pattern = '/(?:(?:\/\*(?:[^*]|(?:\*+[^*\/]))*\*+\/)|(?:(?<!\:|\\\|\'|\")\/\/.*))/';
// $output = preg_replace($pattern, '', $output);
$Search = array(
'/\/\/\ .*/',
'/(\n|^)(\x20+|\t)/',
'/(\n|^)\/\/(.*?)(\n|$)/',
'/\n/',
'/\<\!--.*?-->/', #same выше
'/(\x20+|\t)/', # Delete multispace (Without \n)
'/\>\s+\</', # strip whitespaces between tags
'/(\"|\')\s+\>/', # strip whitespaces between quotation ("') and end tags
'/=\s+(\"|\')/',
'/ (\t| )+/', # strip whitespaces between = "'
);
$Replace = array(
"",
"\n",
"\n",
" ",
"",
" ",
"><",
"$1>",
"=$1",
""
);
$output = preg_replace($Search,$Replace,$output);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment