• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

jenstornell/tiny-html-minifier: Minify HTML in PHP with just a single class

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

jenstornell/tiny-html-minifier

开源软件地址:

https://github.com/jenstornell/tiny-html-minifier

开源编程语言:

PHP 80.7%

开源软件介绍:

Tiny Html Minifier

Version 2.2 MIT license Donate

Changelog

In short

  • A HTML minifier in PHP.
  • It's really really fast.
  • Only 1 file is required.
  • Almost no regular expressions.
  • Almost no options.

Details - What the minifier does

  • Remove HTML comments.
  • Remove slash in self closing elements. /> becomes >.
  • Remove type="text/css" and type="text/javascript" in style and script tags.
  • Minimize elements within <head></head>. It will not keep any whitespace (except inside script).
  • Minimize elements within <body></body> but keep spaces between tags to preserve inline data (optional).
  • Minimize inline SVG files (which are a bunch of XML tags).
  • Minimize Custom Elements. They look like this: <my-element>My content</my-element>.
  • Skip code, pre, script and textarea from being minified.

Install & usage

1. Download

ZIP

Download tiny-html-minifier.php or the whole ZIP.

Composer

You can install it with Composer as well.

2. Add the code

<?php
require 'tiny-html-minifier.php';
echo TinyMinify::html($html);

Before / after

Before

<!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>

After

<!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>

Options

<?php
require 'tiny-html-minifier.php';
echo TinyMinify::html($html, $options = [
    'collapse_whitespace' => false,
    'disable_comments' => false,
]);

collapse_whitespace

Not collapsed

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>

Collapsed

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>

Requirements

  • PHP7+

Disclaimer

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.

License

MIT

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.

Donate

If you want to make a donation, you can do that by sending any amount https://www.paypal.me/DevoneraAB

Credits




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap