Created
November 22, 2015 22:36
-
-
Save frugan-dev/280ca9ff575a7e5db476 to your computer and use it in GitHub Desktop.
Silex minify HTML middleware
This file contains hidden or 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 | |
$app->after(function (Request $request, Response $response) use ($app) { | |
if ($app['debug']) { | |
return; | |
} | |
if (0 === strpos($response->headers->get('Content-Type'), 'text/html')) { | |
$search = array('/\n/','/\>[^\S ]+/s','/[^\S ]+\</s','/(\s)+/s'); | |
$replace = array(' ','>','<','\\1'); | |
$minified_content = preg_replace($search, $replace, $response->getContent()); | |
$response->setContent($minified_content); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment