Skip to content

Instantly share code, notes, and snippets.

@miguelbalboa
Forked from garagesocial/filters.php
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelbalboa/9224ecc1daeadff5ab49 to your computer and use it in GitHub Desktop.
Save miguelbalboa/9224ecc1daeadff5ab49 to your computer and use it in GitHub Desktop.
<?php
### --- Snip --- ###
App::after(function($request, $response)
{
// HTML Minification
if(App::Environment() != 'local')
{
if($response instanceof Illuminate\Http\Response && str_contains($response->headers->get('content-type'), 'text/html'))
{
$output = $response->getOriginalContent();
$filters = array(
'/<!--([^\[|(<!)].*)/' => '', // Remove HTML Comments (breaks with HTML5 Boilerplate)
'/(?<!\S)\/\/\s*[^\r\n]*/' => '', // Remove comments in the form /* */
'/\s{2,}/' => ' ', // Shorten multiple white spaces
'/(\r?\n)/' => '', // Collapse new lines
);
$output = preg_replace(array_keys($filters), array_values($filters), $output);
$response->setContent($output);
}
}
});
### --- Snip --- ###
@miguelbalboa
Copy link
Author

Verify that the response header is text/html, to avoid problems when returning an image for example.

@EmilMoe
Copy link

EmilMoe commented Oct 26, 2014

Sorry to say, but this broke all text on my page.

@miguelbalboa
Copy link
Author

Hello @EmilMoe, This is for Laravel, and you put is code in global.php file,
You can see original gist in https://gist.github.com/zmsaunders/5619519.
¡Saludos!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment