gymadarasz / xparser
PHP中的快速XHTML DOM解析器
Requires (Dev)
- gymadarasz/minitest: dev-master
- symfony/css-selector: ^3.0
This package is auto-updated.
Last update: 2024-08-29 04:14:03 UTC
README
XParser是一个非常快速且易于使用的XHTML DOM解析器。
如果你喜欢在PHP后端使用jQuery风格的DOM选择,或者你喜欢simple_html_dom或Ganon,你一定会喜欢这个智能库!
为什么你应该使用它?
- 基准
[2015-12-30 04:12:58] Measuring Simple HTML DOM Parser...
[2015-12-30 04:12:59] Time: 855ms 0.85500001907349
[2015-12-30 04:12:59] Memory usage: 1.75Mb 1835008
[2015-12-30 04:12:59] Memory peak: 1.75Mb 1835008
[2015-12-30 04:12:59]
[2015-12-30 04:12:59] Measuring XParser...
[2015-12-30 04:12:59] Time: 67ms 0.067000150680542
[2015-12-30 04:12:59] Memory usage: 1.75Mb 1835008
[2015-12-30 04:12:59] Memory peak: 1.75Mb 1835008
[2015-12-30 04:12:59]
[2015-12-30 04:12:59] Measuring Ganon...
[2015-12-30 04:13:00] Time: 917ms 0.91700005531311
[2015-12-30 04:13:00] Memory usage: 3.75Mb 3932160
[2015-12-30 04:13:00] Memory peak: 3.75Mb 3932160
[2015-12-30 04:13:00]
[2015-12-30 04:13:00] Symfony CSS Selector combined with DOMDocument and DOMXPath...
[2015-12-30 04:13:00] Time: 129ms 0.12899994850159
[2015-12-30 04:13:00] Memory usage: 4.25Mb 4456448
[2015-12-30 04:13:00] Memory peak: 4.25Mb 4456448
和
- PHP 5.6
- Composer & PSR-4 支持
- 通过Minitest进行单元测试
- 通过SensioLabsInsight进行PHP-Quality测试
通过Composer安装
$ composer require gymadarasz/xparser
或下载最新版本
使用方法
<?php include 'vendor/autoload.php'; // load a DOM root form a string or file/url $x = new gymadarasz\xparser\XNode(file_get_contents('http://your-important-document-or-template.com')); // select elements via simple CSS selector and read attributs or manipulate contents easily e.g.: $x('a#your_link')->inner('Hello world')->href = 'http://your.hellopage.com'; // or make a foreach on all elements foreach($x('div.hello') as $index => $hellodiv) { $hellodiv->inner('It is the ' . $index . 'th Hello DIV!'); } // or just use jQuery style 'each' function, it's same $x('#nav a', function($elem) { $elem->href = '//myurl/' . $elem->href; }); // show document echo $x;
验证
验证不是一个太快的进程,你不必在每次加载和解析HTML时都使用它,但如果你不确定你的HTML是否有效,你可以在使用前检查它。
if($x->validate()) { // ..do something here $x->find('.hello2')->outer(); } else { // your html contains an invalid closure structure die('Invalid document!'); }
注意:小心!
从技术上讲,当你执行一个尝试在HTML结构中找到递归的查询时,正则表达式的搜索时间会呈指数级提高,但如果你尝试找到精确的元素,它似乎非常快。所以,如果你知道你要找的元素(或几个元素)在哪里,并且这些元素在DOM树中不是太深,这个库会很快。
在我们尝试处理它之前,我们永远无法知道递归中会发生什么,所以我不得不在元素搜索函数中实现递归检测。它将作为一个类变量在XNode中工作,直到获取到深层次的进程。所以,如果你需要默认的更深入搜索,在private static $maxRecursionInStartegy = 8;
你可以修改它,但请注意!
获取元素数量
当你不知道你的查询中会有多少个元素时,我创建了一个getCount
函数,它相对较快,可以告诉你选择中有多少个元素。
// it will be return how many <div> element are in your xhtml $count = $xnode->getCount('div');
获取父元素
$span = $x->find('span', 0); $parent = $span->getParent();
其他功能
// make a DOM node element $x = new XNode('<p>Your XHTML or XML here...</p>'); // get any attribute $attr = $x->attr('href'); // same but shorten: $attr = $x->href; // set any attributes $x->attr('href', 'link-here'); // or just: $x->href = 'link-here'; // find elements in the dom struct, e.g. find all <div> elements: $x->find('div'); // or find an n-th element only: $x->find('div', 3); // or sort format: $x('div'); // here, you can use complex css selection e.g: $x('div#first, div#sixth, div.selected') // or you can use typically method calls $x->getElementById('element01'); $x->getElements($tagName, $attributeRegex, $attributeValueRegex); // <- all parameters are optional $x->getElementsByClass('class-name'); // get/set element inner text: $inner = $x->inner(); $x->inner('Hello World!'); // get/set element outer text: $outer = $x->outer(); $x->outer('It\'ll replace the element!');
实现了Symfony CSS-Selector
如果你想在你的查询中使用更多的CSS选择,更多的选项,例如 '>' 子选择和 'nth-child' 或通过属性选择等,这是可能的,通过Symfony CSS-Selector DOMDocument和XPath。
注意:我的目标不是要做出比symphony更好的CSS选择。我想做一个真正的快速HTML阅读器和/或PHP库,CSS选择只是这个库中的一个“额外”功能。如果你有任何想法如何改进它,请在GitHub上留下一个问题。