Stars: 164
Forks: 86
Pull Requests: 10
Issues: 34
Watchers: 11
Last Updated: 2023-04-30 08:57:33
Minify HTML in PHP with just a single class
License: MIT License
Languages: PHP, HTML
/>
becomes >
. type="text/css"
and type="text/javascript"
in style
and script
tags.<head></head>
. It will not keep any whitespace (except inside script
).<body></body>
but keep spaces between tags to preserve inline data (optional).<my-element>My content</my-element>
.code
, pre
, script
and textarea
from being minified.ZIP
Download tiny-html-minifier.php
or the whole ZIP.
Composer
You can install it with Composer as well.
<?php
require 'tiny-html-minifier.php';
echo TinyMinify::html($html);
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link href="http://example.com/style.css" rel="stylesheet" />
<link rel="icon" href="http://example.com/favicon.png" />
<title>Tiny Html Minifier</title>
</head>
<body class="body">
<div class="main-wrap">
<main>
<textarea>
Some text
with newlines
and some spaces
</textarea>
<div class="test">
<p>This text</p>
<p>should not</p>
<p>wrap on multiple lines</p>
</div>
</main>
</div>
<script>
console.log('Script tags are not minified');
console.log('This is inside a script tag');
</script></body>
</html>
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1.0"><link href="http://example.com/style.css" rel="stylesheet"><link rel="icon" href="http://example.com/favicon.png"><title>Tiny Html Minifier</title></head> <body class="body"><div class="main-wrap"> <main> <textarea>
Some text
with newlines
and some spaces
</textarea> <div class="test"> <p>This text</p> <p>should not</p> <p>wrap on multiple lines</p> </div> </main> </div> <script>
console.log('Script tags are not minified');
console.log('This is inside a script tag');
</script></body></html>
<?php
require 'tiny-html-minifier.php';
echo TinyMinify::html($html, $options = [
'collapse_whitespace' => false,
'disable_comments' => false,
]);
Spaces are preserved (except for most elements within <head></head>
). It's good when using the elements inline. This is the default.
<ul><li> <a href="#"> My link </a></li><li> <a href="#"> My link </a></li> </ul>
Spaces are collapsed. The text inside the element is still untouched. Set this value to true
and you will save a few extra bytes.
<ul><li><a href="#">My link</a></li><li><a href="#">My link</a></li></ul>
This plugin is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.
It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.
If you want to make a donation, you can do that by sending any amount https://www.paypal.me/DevoneraAB