Skip to content

Instantly share code, notes, and snippets.

@mayoz
Forked from allaniftrue/filters.php
Created May 22, 2014 20:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mayoz/71bfe01bc1c93da4eda8 to your computer and use it in GitHub Desktop.
Save mayoz/71bfe01bc1c93da4eda8 to your computer and use it in GitHub Desktop.
<?php
App::after(function($request, $response)
{
/**
* HTML Minification
* https://gist.github.com/zmsaunders/5619519
* https://gist.github.com/garagesocial/6059962
*/
if (Config::get('project.minify.html', false) !== false)
{
if($response instanceof Illuminate\Http\Response)
{
$filters = array(
// remove HTML comments except IE conditions
'/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s' => '',
// remove comments in the form /* */
'/(?<!\S)\/\/\s*[^\r\n]*/' => '',
// shorten multiple white spaces
'/>\s{2,}</' => '><',
// shorten multiple white spaces
'/\s{2,}/' => ' ',
// collapse new lines
'/(\r?\n)/' => '',
);
$output = $response->getOriginalContent();
$output = preg_replace(array_keys($filters), array_values($filters), $output);
$response->setContent($output);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment