Skip to content

Instantly share code, notes, and snippets.

@frugan-dev
Created November 22, 2015 22:36
Show Gist options
  • Save frugan-dev/280ca9ff575a7e5db476 to your computer and use it in GitHub Desktop.
Save frugan-dev/280ca9ff575a7e5db476 to your computer and use it in GitHub Desktop.
Silex minify HTML middleware
<?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