duncan3dc/domparser
Composer 安装命令:
composer require duncan3dc/domparser
包简介
Wrappers for the standard DOMDocument class to provide extra functionality for html/xml parsing
README 文档
README
Wrappers for the PHP DOMDocument class to provide extra functionality for html/xml parsing
Constructor Arguments
There are 2 constructors available, one for the HtmlParser class and one for the XmlParser class, they both work in the same way.
- Only 1 parameter is available, which should be passed as a string, and contain the content to parse (xml/html)
- Warnings are captured during the loading of the content using libxml_use_internal_errors() and libxml_get_errors(), these are available from the errors property after the class has been initiated
Public Properties
- html/xml (string) - Depending on which class was used one of these properties will be present and contain the content used for parsing
- errors (array) - If any errors were encountered during parsing they will be in this array (see [Constructor Arguments])
- dom (DOMDocument) - This is the internal instance of the DOMDocument class used
- mode (string) - Indicates whether the parser is operating in html or xml mode
Public Methods
- getTags(string $tagName): array - Similar to DOMDocument::getElementsByTagName() except a standard array is returned.
Alias: getElementsByTagName() - getTag(string $tagName): Element - Similar to getTags() but will return the first element matched, instead of an array of elements.
Alias: getElementByTagName() - getElementsByClassName(mixed $className): array - Matches elements that have a class attribute that matches the string parameter.
If you want to find elements with multiple classes, pass the $className as an array of classes, and only elements that have all classes will be returned - getElementByClassName(mixed $className): Element - Similar to getElementsByClassName() except this will return the first element matched
- output(): string - Returns the xml/html repesented by the receiver, formatted using DOMDocument::formatOutput
- xpath(string $path): array - Returns an array of elements matching the $path
Dom Elements
All of the methods that return elements return them as instances of the Element class, this acts similarly to the standard DOMElement class, except it has all of the above methods available, and the nodeValue property has leading and trailing whitespace removed using trim()
Examples
The parser classes use a namespace of duncan3dc\Dom
use duncan3dc\Dom\Html\Parser; use duncan3dc\Dom\Xml\Parser;
$parser = new Parser(file_get_contents("http://example.com")); echo "Page Title: " . $parser->getTag("title")->nodeValue . "\n"; $contentType = false; $meta = $parser->getTags("meta"); foreach($meta as $element) { if($element->getAttribute("http-equiv") == "Content-type") { $contentType = $element->getAttribute("content"); } } echo "Content Type: " . (($contentType) ?: "NOT FOUND") . "\n";
duncan3dc/domparser for enterprise
Available as part of the Tidelift Subscription
The maintainers of duncan3dc/domparser and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.
duncan3dc/domparser 适用场景与选型建议
duncan3dc/domparser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 408.41k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2014 年 05 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xml」 「parser」 「html」 「parsing」 「generation」 「writer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 duncan3dc/domparser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 duncan3dc/domparser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 duncan3dc/domparser 相关的其它包
同方向 / 同关键字的高下载量 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
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Load DOM document safety
An MT940 bank statement parser for PHP
HTML and form generation
Laravel integration for the jsonapi.org parser
统计信息
- 总下载量: 408.41k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 26
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2014-05-31