wa72/html-pretty-min
Composer 安装命令:
composer require wa72/html-pretty-min
包简介
HTML minifier and indenter that works on the DOM tree
README 文档
README
HTML Pretty-Min is a PHP library for minifying and prettyprinting (indenting) HTML documents that works directly on the DOM tree of an HTML document.
Currently, it has the following features:
-
Prettyprint:
- Indent Block-level elements, do not indent inline elements
-
Minify:
- Remove whitespace and newlines
- Compress embedded Javascript using mrclay/jsmin-php
- Compress embedded CSS using tubalmartin/cssmin
- Remove some attributes when their value is empty (by default "style" and "class" attributes)
- Remove comments, except those matching some given regular expressions (by default, IE conditional comments are kept)
Installation
HTML Pretty-Min is listed on Packagist.
composer require wa72/html-pretty-min
Usage
<?php use Wa72\HtmlPrettymin\PrettyMin; $pm = new PrettyMin(); $output = $pm ->load($html) // $html may be a \DOMDocument, a string containing an HTML code, // or an \SplFileInfo pointing to an HTML document ->minify() ->saveHtml();
For prettyprinting, call the indent() method instead of minify().
The PrettyMin() constructor can be given an associative options array. Here are the possible option keys and their default values:
'minify_js' => true,
'minify_css' => true,
'remove_comments' => true,
'remove_comments_exeptions' => ['/^\[if /'],
'keep_whitespace_around' => [
// keep whitespace around all inline elements
'b', 'big', 'i', 'small', 'tt',
'abbr', 'acronym', 'cite', 'code', 'dfn', 'em', 'kbd', 'strong', 'samp', 'var',
'a', 'bdo', 'br', 'img', 'map', 'object', 'q', 'span', 'sub', 'sup',
'button', 'input', 'label', 'select', 'textarea'
],
'keep_whitespace_in' => ['script', 'style', 'pre'],
'remove_empty_attributes' => ['style', 'class'],
'indent_characters' => "\t"
Attention: Because the formatting is done directly on the DOM tree, a DOMDocument object given to the load() method
will be modified:
$dom_document = new \DOMDocument('1.0', 'UTF-8'); $dom_document->loadHTML('<html>...some html code...</html>'); $pm->load($dom_document)->minify(); echo $dom_document->saveHTML(); // Will output the minified, not the original, document
wa72/html-pretty-min 适用场景与选型建议
wa72/html-pretty-min 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 231.94k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2015 年 11 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wa72/html-pretty-min 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wa72/html-pretty-min 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 231.94k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 30
- 依赖项目数: 10
- 推荐数: 4
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-05