fu Tape/html-utility

HTML实用工具库

1.0.0 2019-12-01 15:00 UTC

This package is auto-updated.

Last update: 2024-09-29 05:35:45 UTC


README

此库提供了一套用于处理HTML标记的实用工具。

实用函数作为抽象类中的静态函数实现,该类不会被实例化。

此外,此库还提供HtmlInliner及其子类SemanticHtmlInliner(见下文)。

安装

composer require futape/html-utility

用法

HTML

此实用工具类提供用于处理HTML标记的函数。

use Futape\Utility\Html\Html;

echo Html::preformatted("PHP is <3\n Foo   Bar\nBaz\tBam ");
// "PHP is &lt;3<br />\n&nbsp;Foo &nbsp; Bar<br />\nBaz&nbsp;&nbsp;&nbsp;&nbsp;Bam&nbsp;"

HtmlInliner

一个将HTML标记转换为纯文本并在行内化任何文本的类。

以下示例来自单元测试。

markup.html

<h1>A markup file</h1>
<p>This is just a file containing HTML markup.</p>
<table>
    <thead>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>John</td>
            <td>Doe</td>
            <td>42</td>
        </tr>
    </tbody>
</table>
<p>A list of very important stuff:</p>
<ul>
    <li>One</li>
    <li>Two</li>
</ul>
<p>And a list of definitions:</p>
<dl>
    <dt>WWW</dt>
    <dd>World Wide Web</dd>
    
    <dt>HTTP</dt>
    <dd>Hypertext Transfer Protocol</dd>
</dl>
<img src="http://example.com/picture.jpg" alt="An example picture" />

inline.php

use Futape\Utility\Html\HtmlInliner;

echo (new HtmlInliner(file_get_contents('./markup.html')))->render();
// "A markup file This is just a file containing HTML markup. A list of very important stuff: One, Two, And a list of definitions: WWW: World Wide Web, HTTP: Hypertext Transfer Protocol,"

SemanticHtmlInliner

这是HtmlInliner的子类,它考虑了HTML元素的语义意义,而不是主要依赖它们的显示。

测试

此库使用PHP Unit进行单元测试。

要执行测试,请安装composer依赖项(包括开发依赖项),切换到tests目录并运行以下命令

../vendor/bin/phpunit