wa72/html-pretty-min 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wa72/html-pretty-min

最新稳定版本:v0.3.1

Composer 安装命令:

composer require wa72/html-pretty-min

包简介

HTML minifier and indenter that works on the DOM tree

README 文档

README

tests Latest Version

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

统计信息

  • 总下载量: 209.92k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 22
  • 点击次数: 3
  • 依赖项目数: 10
  • 推荐数: 4

GitHub 信息

  • Stars: 22
  • Watchers: 3
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固