A PHP package to minify HTMLs, Javascripts and CSSs. Supports PSR-4 Autoloader.
Via Composer
$ composer require doncadavona/html_minifier
use DonCadavona\HtmlMinifier\HtmlMinifier;
// Sample HTML string
$html = '
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
html {
padding-top: 20px;
}
body {
margin: 0px;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<script>
// This is single-line a comment.
/* This is a
multi-line
comment. */
/**
* This is a doc block comment.
*/
</script>
</body>
</html>
';
// Minify the HTML
echo HtmlMinifier::minify($html);
// <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style type="text/css"> html { padding-top: 20px; } body { margin: 0px; } </style></head><body><h1>Hello World</h1><script></script></body></html>
// Minify the HTML and remove JavaScripts and CSSs:
echo HtmlMinifier::minify($html, true, true);
// <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title></head><body><h1>Hello World</h1></body></html>
Please see CHANGELOG for more information on what has changed recently.
$ composer test
Please see CONTRIBUTING and CONDUCT for details.
If you discover any security related issues, please email [email protected]
instead of using the issue tracker.
- Oleg Gheorghita for his yii2-htmlcompress package.
The MIT License (MIT). Please see License File for more information.