originphp/html
Composer 安装命令:
composer require originphp/html
包简介
OriginPHP Html
README 文档
README
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 originphp/html 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
Caching and compression for Twig assets (JavaScript and CSS).
CSS & JavaScript minifier for Silverstripe
A package for pack css and javascript files
Modification of rmrevin/yii-2-minify-view lib to allow minification of css & js in a load balanced environment
统计信息
- 总下载量: 12.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 17
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-12