OriginPHP Html

2.0.0 2021-01-04 10:16 UTC

This package is auto-updated.

Last update: 2024-09-04 18:01:39 UTC


README

license build coverage

Html类为处理HTML提供了一些有用的功能。

安装

要安装此包

$ composer require originphp/html

然后将其添加到你的文件中

use Origin\Html\Html;

从文本

如果你需要将文本块转换为HTML

$text = <<< EOF
This is a paragrpah.
This is another line part of the paragraph.

This is a new paragraph.
EOF;
$html = Html::fromText($text);

这将创建

<p>This is a paragrpah.<br>This is another line part of the paragraph.</p>
<p>This is a new paragraph.</p>

如果你想要段落使用不同于p的标签包裹,可以这样做

$html = Html::fromText($text,['tag'=>'div']);

转换为文本

你也可以将HTML字符串转换为格式化文本

$text = Html::toText($html);

例如

$html = <<< EOF
<h1>Search Engines</h1>
<h2>Google</h2><h3>About</h3>
<blockquote>Google is not a conventional company. We do not intend to become one.</blockquote>
<p>Google LLC is an American        multinational technology 
company that specializes in Internet-related services and products, which include online advertising technologies, search engine, cloud computing, software, and hardware.<br>It is considered one of the Big Four technology companies, alongside Amazon, Apple and Facebook.</p>
<p>Benefits of using Google:</p>
<ol>
    <li>Good quality search results</li>
    <li>Relevent advertising</li>
</ol>
<p>Important links:</p>
<ul>
    <li><a href="https://en.wikipedia.org/wiki/Google">Google's Wikipedia Page</a></li>
    <li><a href="https://abc.xyz/">Alphabet</a></li>
</ul>
<h3>Financial Results</h3>
<p>Below are the <span>financial</span> results for the last <em>3 years</em>.</p>
<table>
<tr>
        <th>Revenue</th>
        <th>31/12/2018</th>
        <th>31/12/2017</th>
        <th>31/12/2016</th>
</tr>
<tr>
        <td>Total revenue</td>
        <td>136,819,000</td>
        <td>110,855,000</td>
        <td>90,272,000</td>
</tr>
<tr>
        <td>Cost of revenue</td>
        <td>59,549,000</td>
        <td>45,583,000</td>
        <td>35,138,000</td>
</tr>
<tr>
        <td>Gross profit</td>
        <td><strong>77,270,000</strong></td>
        <td><strong>65,272,000</strong></td>
        <td><strong>55,134,000</strong></td>
</tr>
</table>
<h3>Using Google API</h3>
<p>You can use the <a href="https://github.com/googleapis/google-api-php-client/tree/master/examples">Google API</a> to access various Google services.</p>
<p>To install the library:</p>
<pre>
<code>composer require google/apiclient:^2.0</code>
</pre>
<p>Create a file called <code>quickstart.php</code> and add the following contents</p>
<pre><code>require __DIR__ . '/vendor/autoload.php';

if (php_sapi_name() != 'cli') {
    throw new Exception('This application must be run on the command line.');
}
... truncated
</code></pre>
EOF;

将输出

Search Engines
==============

Google
------

About
-----

"Google is not a conventional company. We do not intend to become one."

Google LLC is an American multinational technology company that specializes in Internet-related services and products, which include online advertising technologies, search engine, cloud computing, software, and hardware.
It is considered one of the Big Four technology companies, alongside Amazon, Apple and Facebook.

Benefits of using Google:

1. Good quality search results
2. Relevent advertising

Important links:

- Google's Wikipedia Page [https://en.wikipedia.org/wiki/Google]
- Alphabet [https://abc.xyz/]

Financial Results
-----------------

Below are the financial results for the last 3 years.

+------------------+--------------+--------------+-------------+
| Revenue          | 31/12/2018   | 31/12/2017   | 31/12/2016  |
+------------------+--------------+--------------+-------------+
| Total revenue    | 136,819,000  | 110,855,000  | 90,272,000  |
| Cost of revenue  | 59,549,000   | 45,583,000   | 35,138,000  |
| Gross profit     | 77,270,000   | 65,272,000   | 55,134,000  |
+------------------+--------------+--------------+-------------+

Using Google API
----------------

You can use the [Google API](https://github.com/googleapis/google-api-php-client/tree/master/examples) to access various Google services.

To install the library:

composer require google/apiclient:^2.0

Create a file called quickstart.php and add the following contents

     require __DIR__ . '/vendor/autoload.php';
     
     if (php_sapi_name() != 'cli') {
         throw new Exception('This application must be run on the command line.');
     }
     ... truncated
     

创建不带格式的文本版本

$text = Html::toText($html,['format'=>false]);

主要区别在于标题、表格、代码等不会被格式化。HTML将被清理,添加换行符,并将列表转换。如果列表有子列表,则将添加缩进。

压缩

压缩清理空格,移除注释,从而压缩HTML字符串。

$minified = Html::minify($html);

以下选项被支持

  • collapseWhitespace: 默认:true。在文本节点中压缩空格
  • conservativeCollapse: 默认:false。始终将空格压缩到至少1个空格
  • collapseInlineTagWhitespace: 默认:false。不要在行内元素之间留下任何空格
  • minifyJs: 默认:false 压缩行内JavaScript(beta)
  • minifyCss: 默认:false 压缩行内CSS(beta)

清理

清理允许在HTML字符串中只允许某些标签和属性。

$html = Html::sanitize($html,[
    'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
    'p',
    'i', 'em', 'strong', 'b', 'blockquote', 'del',
    'a' => ['href'],
    'ul', 'li', 'ol', 'br',
    'code', 'pre',
    'img' => ['src','alt']]
    );

删除标签

要从HTML字符串中删除选定的标签及其内容,换句话说,从黑名单中删除标签

$html = Html::stripTags($html,['script','iframe','img']);

转义

当显示用户输入的HTML时,为了安全起见,必须正确转义,有关更多信息,请参阅跨站脚本

$escaped = Html::escape($html);