wongyip/html-beautify

HTML 格式化工具

v1.1.0 2024-04-22 06:21 UTC

This package is auto-updated.

Last update: 2024-09-23 16:36:52 UTC


README

基于Ivan Weiler的Beautify HTML,我在这里将其带到Packagist,没有添加任何功能。请查阅原始的README以获取详细信息。

安装

composer require wongyip/html-beautify

用法

保持了与原始的Beautify HTML相同的用法,除了命名空间化的类名。在大多数情况下(至少在我的大多数情况下),Beautify::class在整个请求/命令生命周期中只使用一次,因此添加了一个静态的Beautify::html()方法,以便以无状态的方式格式化HTML。

use \Wongyip\HTML\Beautify;

$html = <<<HTML
    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head>
    <body><div class="row col-sm12"><ol><li></li><li></li><li></li></ol></div></body></html>
    HTML;

# State-less
echo Beautify::html($html);

# Reusable
$beautifier = new Beautify();
echo $beautifier->beautify($html);

# Alternative static constructor
echo Beautify::init()->beautify($html);

上述所有echo语句输出以下相同的HTML

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>

<body>
<div class="row" style="color: brown;">
    <ul>
        <li>Hello</li>
        <li>World!</li>
    </ul>
</div>
</body>

</html>

选项

use \Wongyip\HTML\Beautify;

# All options are optional.
$options = [
    'indent_inner_html'     => false,
    'indent_char'           => " ",
    'indent_size'           => 4,
    'wrap_line_length'      => 32768,
    'unformatted'           => ['code', 'pre'],
    'preserve_newlines'     => false,
    'preserve_newlines_max' => 32768,
    'indent_scripts'        => 'normal',
];

# Set on instantiate.
$beautifier = new Beautify($options);

# Update option(s)
$beautifier->options(['indent_size' => 2]);

# Get options
$options = $beautifier->options();

试试看

composer create-project wongyip/html-beautify
cd html-beautify
composer install
php demo/demo.php