arc/html
Composer 安装命令:
composer require arc/html
包简介
Ariadne Component Library: html writer and parser Component
关键字:
README 文档
README
arc/html
This component provides a unified html parser and writer. The writer allows for readable and correct html in code, not using templates. The parser is a wrapper around both DOMDocument and SimpleXML.
The parser and writer also work on fragments of HTML. The parser also makes sure that the output is identical to the input. When converting a node to a string, \arc\html will return the full html string, including tags. If you don't want that, you can always access the 'nodeValue' property to get the original SimpleXMLElement.
Finally the parser also adds the ability to use basic CSS selectors to find elements in the HTML.
use \arc\html as h; $htmlString = h::doctype() .h::html( h::head( h::title('Example site') ), h::body( ['class' => 'homepage'], h::h1('An example site') ) );
$html = \arc\html::parse($htmlString); $title = $html->head->title->nodeValue; // SimpleXMLElement 'Example site' $titleTag = $html->head->title; // <title>Example site</title>
CSS selectors
$title = current($html->find('title'));
The find() method always returns an array, which may be empty. By using current() you get the first element found, or null if nothing was found.
The following CSS selectors are supported:
tag1 tag2
This matchestag2which is a descendant oftag1.tag1 > tag2
This matchestag2which is a direct child oftag1.tag:first-child
This matchestagonly if its the first child.tag1 + tag2
This matchestag2only if its immediately preceded bytag1.tag1 ~ tag2
This matchestag2only if it has a previous sibling tag1.tag[attr]
This matchestagif it has the attributeattr.tag[attr="foo"]
This matchestagif it has the attributeattrwith the valuefooin its value list.tag#id
This matches anytagwith idid.#id
This matches any element with idid.tag.class-name
Matches anytagwith a classclass-name..class-name
Matches any element with a classclass-name.
SimpleXML
The parsed HTML behaves almost identical to a SimpleXMLElement, with the exceptions noted above. So you can access attributes just like SimpleXMLElement allows:
$class = $html->html->body['class']; $class = $html->html->body->attributes('version');
You can walk through the node tree:
$title = $html->html->head->title;
Any method or property available in SimpleXMLElement is included in \arc\html parsed data.
DOMElement
In addition to SimpleXMLElement methods, you can also call any method and most properties available in DOMElement.
$class = $html->html->body->getAttributes('class'); $title = current($html->getElementsByTagName('title'));
Parsing fragments
The arc\html parser also accepts partial HTML content. It doesn't require a single root element.
$htmlString = <<< EOF <li> <a href="anitem/">An item</a> </li> <li> <a href="anotheritem/">Another item</a> </li> EOF; $html = \arc\html::parse($htmlString); $links = $html->find('a');
And when you convert the html back to a string, it will still be a partial HTML fragment.
If you parse a single HTML tag, other than <html>, you must still reference this element to access it:
$htmlString = <<< EOF <ul> <li> <a href="anitem/">An item</a> </li> <li> <a href="anotheritem/">Another item</a> </li> </ul> EOF; $html = \arc\html::parse($htmlString); $ul = $html->ul;
Why use this instead of DOMDocument or SimpleXML?
arc\html::parse has the following differences:
- When converted to string, it returns the original HTML, without additions you didn't make.
- You can use it with partial HTML fragments.
- No need to remember calling importNode() before appendChild() or insertBefore()
- No need to switch between SimpleXML and DOMDocument, because you need that one method only available in the other API.
- When returning a list of elements, you always get a simple Array, not a magic NodeList.
In addition arc\html doubles as a simple way to generate valid and indented HTML, with readable and self-validating code.
arc/html 适用场景与选型建议
arc/html 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 321 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 01 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「components」 「component」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 arc/html 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arc/html 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 arc/html 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
FSi DataSource Component
Web Font Loader gives you added control when using linked fonts via @font-face.
Priveate for SkeekS CMS
Ariadne Component Library: xml writer and parser Component
Ariadne Component Library: Cache Component
Ariadne Component Library: hierarchical configuration management Component
统计信息
- 总下载量: 321
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 9
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-17