Skip to content

Instantly share code, notes, and snippets.

@udovichenko
Created August 31, 2018 10:41
Show Gist options
  • Save udovichenko/c9da4722eefb4088a5348235d3620088 to your computer and use it in GitHub Desktop.
Save udovichenko/c9da4722eefb4088a5348235d3620088 to your computer and use it in GitHub Desktop.
MODX Plugin htmlMinify
<?php
$e =& $modx->event;
switch ($e->name) {
case "OnWebPagePrerender": {
$output = &$modx->resource->_output;
$search = array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\</s', // strip whitespaces before tags, except space
'/(\s)+/s', // shorten multiple whitespace sequences
'/<!--(.|\s)*?-->/' // Remove HTML comments
);
$replace = array(
'>',
'<',
'\\1',
''
);
$output = preg_replace($search, $replace, $output);
$modx->resource->_output = $output;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment