src
3 years ago
tests
3 years ago
changelog.md
3 years ago
composer.json
3 years ago
license
3 years ago
readme.md
3 years ago
readme.md
150 lines
| 1 | # Tiny Html Minifier |
| 2 | |
| 3 |   [](https://www.paypal.me/DevoneraAB](https://www.paypal.me/DevoneraAB](https://www.paypal.me/DevoneraAB) |
| 4 | |
| 5 | [](changelog.mdChangelog](changelog.md](changelog.md) |
| 6 | |
| 7 | ## In short |
| 8 | |
| 9 | - A HTML minifier in PHP. |
| 10 | - It's really really fast. |
| 11 | - Only 1 file is required. |
| 12 | - Almost no regular expressions. |
| 13 | - Almost no options. |
| 14 | |
| 15 | ## Details - What the minifier does |
| 16 | |
| 17 | - Remove HTML comments. |
| 18 | - Remove slash in self closing elements. ` />` becomes `>`. |
| 19 | - Remove ` type="text/css"` and `type="text/javascript"` in `style` and `script` tags. |
| 20 | - Minimize elements within `<head></head>`. It will not keep any whitespace (except inside `script`). |
| 21 | - Minimize elements within `<body></body>` but keep spaces between tags to preserve inline data (optional). |
| 22 | - Minimize inline SVG files (which are a bunch of XML tags). |
| 23 | - Minimize Custom Elements. They look like this: `<my-element>My content</my-element>`. |
| 24 | - Skip `code`, `pre`, `script` and `textarea` from being minified. |
| 25 | |
| 26 | ## Install & usage |
| 27 | |
| 28 | ### 1. Download |
| 29 | |
| 30 | **ZIP** |
| 31 | |
| 32 | Download `tiny-html-minifier.php` or the whole ZIP. |
| 33 | |
| 34 | **Composer** |
| 35 | |
| 36 | You can install it with Composer as well. |
| 37 | |
| 38 | ### 2. Add the code |
| 39 | |
| 40 | ```php |
| 41 | <?php |
| 42 | require 'tiny-html-minifier.php'; |
| 43 | echo TinyMinify::html($html); |
| 44 | ``` |
| 45 | |
| 46 | ## Before / after |
| 47 | |
| 48 | ### Before |
| 49 | |
| 50 | ```html |
| 51 | <!doctype html> |
| 52 | <html lang="en"> |
| 53 | <head> |
| 54 | |
| 55 | <meta charset="utf-8" /> |
| 56 | <meta name="viewport" content="width=device-width,initial-scale=1.0" /> |
| 57 | |
| 58 | <link href="http://example.com/style.css" rel="stylesheet" /> |
| 59 | <link rel="icon" href="http://example.com/favicon.png" /> |
| 60 | |
| 61 | <title>Tiny Html Minifier</title> |
| 62 | |
| 63 | </head> |
| 64 | <body class="body"> |
| 65 | |
| 66 | <div class="main-wrap"> |
| 67 | <main> |
| 68 | <textarea> |
| 69 | Some text |
| 70 | with newlines |
| 71 | and some spaces |
| 72 | </textarea> |
| 73 | |
| 74 | <div class="test"> |
| 75 | <p>This text</p> |
| 76 | <p>should not</p> |
| 77 | <p>wrap on multiple lines</p> |
| 78 | </div> |
| 79 | </main> |
| 80 | </div> |
| 81 | <script> |
| 82 | console.log('Script tags are not minified'); |
| 83 | console.log('This is inside a script tag'); |
| 84 | </script></body> |
| 85 | </html> |
| 86 | ``` |
| 87 | |
| 88 | ### After |
| 89 | |
| 90 | ```html |
| 91 | <!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> |
| 92 | Some text |
| 93 | with newlines |
| 94 | and some spaces |
| 95 | </textarea> <div class="test"> <p>This text</p> <p>should not</p> <p>wrap on multiple lines</p> </div> </main> </div> <script> |
| 96 | console.log('Script tags are not minified'); |
| 97 | console.log('This is inside a script tag'); |
| 98 | </script></body></html> |
| 99 | ``` |
| 100 | |
| 101 | ## Options |
| 102 | |
| 103 | ```php |
| 104 | <?php |
| 105 | require 'tiny-html-minifier.php'; |
| 106 | echo TinyMinify::html($html, $options = [ |
| 107 | 'collapse_whitespace' => false, |
| 108 | 'disable_comments' => false, |
| 109 | ]); |
| 110 | ``` |
| 111 | |
| 112 | ### collapse_whitespace |
| 113 | |
| 114 | #### Not collapsed |
| 115 | |
| 116 | Spaces are preserved (except for most elements within `<head></head>`). It's good when using the elements inline. This is the default. |
| 117 | |
| 118 | ```html |
| 119 | <ul><li> <a href="#"> My link </a></li><li> <a href="#"> My link </a></li> </ul> |
| 120 | ``` |
| 121 | |
| 122 | #### Collapsed |
| 123 | |
| 124 | Spaces are collapsed. The text inside the element is still untouched. Set this value to `true` and you will save a few extra bytes. |
| 125 | |
| 126 | ```html |
| 127 | <ul><li><a href="#">My link</a></li><li><a href="#">My link</a></li></ul> |
| 128 | ``` |
| 129 | |
| 130 | ## Requirements |
| 131 | |
| 132 | - PHP7+ |
| 133 | |
| 134 | ## Disclaimer |
| 135 | |
| 136 | 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 [](https://github.com/jenstornell/tiny-html-minifier/issues/newcreate a new issue](https://github.com/jenstornell/tiny-html-minifier/issues/new](https://github.com/jenstornell/tiny-html-minifier/issues/new). |
| 137 | |
| 138 | ## License |
| 139 | |
| 140 | [](https://github.com/jenstornell/tiny-html-minifier/blob/master/licenseMIT](https://github.com/jenstornell/tiny-html-minifier/blob/master/license](https://github.com/jenstornell/tiny-html-minifier/blob/master/license) |
| 141 | |
| 142 | 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. |
| 143 | |
| 144 | ## Donate |
| 145 | |
| 146 | If you want to make a donation, you can do that by sending any amount https://www.paypal.me/DevoneraAB |
| 147 | |
| 148 | ## Credits |
| 149 | |
| 150 | - [](https://github.com/jenstornellJens Törnell](https://github.com/jenstornell](https://github.com/jenstornell) |