定制 originphp/html 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

originphp/html

Composer 安装命令:

composer require originphp/html

包简介

OriginPHP Html

README 文档

README

license build coverage

The Html class provides some useful functions for when working with HTML.

Installation

To install this package

$ composer require originphp/html

Then add to your file

use Origin\Html\Html;

From Text

If you need to convert a text block to 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);

This will create:

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

If want paragraphs to be wrapped with a different tag than p then would do so like this

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

To Text

You can also convert a HTML string to formatted text

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

For example

$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;

Will output

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
     

To create text version without formatting:

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

The main difference is headings, tables, code etc are not formatted. The HTML is cleaned up, line breaks are added, and lists are converted. If a list has a sublist then indentation will be added.

Minify

Minify cleans up the spacing, removes comments and thus minifies a HTML string.

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

The following options are supported

  • collapseWhitespace: default:true. Collapse whitespace in the text nodes
  • conservativeCollapse: default:false. Always collapse whitespace to at least 1 space
  • collapseInlineTagWhitespace: default:false. Don't leave any spaces between inline elements.
  • minifyJs: default:false minifies inline Javascript (beta)
  • minifyCss: default:false minifies inline CSS (beta)

Sanitize

Sanitize enables to only allow certain tags and attributes in a HTML string.

$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']]
    );

Strip Tags

To strip selected tags and their content from a HTML string, in other words strip tags that are in a blacklist

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

Escape

It is important when displaying user inputted HTML that it is escaped properly for security reasons, see Cross-site scripting for more information.

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

originphp/html 适用场景与选型建议

originphp/html 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.04k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2019 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「html」 「minify」 「compress」 「sanitize」 「html2text」 「originPHP」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 originphp/html 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 originphp/html 我们能提供哪些服务?
定制开发 / 二次开发

基于 originphp/html 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-10-12