Skip to content

Instantly share code, notes, and snippets.

@su-narthur
Forked from frugan-dev/middleware.php
Created October 18, 2016 18:52
Show Gist options
  • Save su-narthur/1930e2e34042317e169016ded74c738a to your computer and use it in GitHub Desktop.
Save su-narthur/1930e2e34042317e169016ded74c738a to your computer and use it in GitHub Desktop.
Silex minify HTML middleware
<?php
$app->after(function (\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\HttpFoundation\Response $response) use ($app) {
if ($app['debug']) return;
$search = array(
'#\/\*.+?\*\/#', // /* JS Comments */
'#^\s+\/\/.+#m', // // JS Comments
'/<!--.+?-->/', // <!-- HTML Comments -->
'/\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