使用DOMXPath和函数查询php [英] use DOMXPath and function query php
本文介绍了使用DOMXPath和函数查询php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有以下html代码部分:
< ul id =tree>
< li>
< a href =>第一个< / a>
< ul>
< li>< a href => subfirst< / a>< / li>
< li>< a href =>亚秒< / a>< / li>
< li>< a href => subthird< / a>< / li>
< / ul>
< / li>
< li>
< a href =>秒< / a>
< ul>
< li>< a href => subfirst< / a>< / li>
< li>< a href =>亚秒< / a>< / li>
< li>< a href => subthird< / a>< / li>
< / ul>
< / li>
< / ul>
需要解析这个html代码并得到下一个(使用DOMXPath :: query()和foreach())
- 第一个
- 第一个
- 亚秒
- 第三个
- second
- subfirst
- subcond
- subthird
部分代码:
$ xpath = new \DOMXPath($ html);
$ catgs = $ xpath-> query('// * [@ id =tree] / li / a');
foreach($ catgs as $ category){
//第一级
echo' - '。 mb_strtolower($ category-> nodeValue)。 <峰; br>;
//第二级
//不知道
}
预先感谢!
解决方案
这是DOMBLAZE的用途:
/ * DOMBLAZE * / $ doc-> registerNodeClass(DOMElement,DOMBLAZE); class DOMBLAZE extends DOMElement {public function __invoke($ expression){return $ this-> xpath($ expression);} function xpath($ expression){$ result =(new DOMXPath($ this-> ownerDocument)) - > ; evaluate($ expression,$ this); return($ result instanceof DOMNodeList)?new IteratorIterator($ result):$ result;}}
$ list = $ doc-> getElementById('tree );
foreach($ list('./ li')as $ item){
echo' - ',$ item('string(./ a)'),\\\
;
foreach($ item('./ ul / li')as $ subitem){
echo' - ',$ subitem('string(./ a)'),\\\
;
$ / code $ / pre
$ b $输出:
- 第一个
- 第一个
- 亚秒
- 第三个
- 第二个
- subfirst
- subcond
- subthird
DOMBLAZE em> 为穷人。
Have the following html code section:
<ul id="tree">
<li>
<a href="">first</a>
<ul>
<li><a href="">subfirst</a></li>
<li><a href="">subsecond</a></li>
<li><a href="">subthird</a></li>
</ul>
</li>
<li>
<a href="">second</a>
<ul>
<li><a href="">subfirst</a></li>
<li><a href="">subsecond</a></li>
<li><a href="">subthird</a></li>
</ul>
</li>
</ul>
Need parse this html code and get next (using DOMXPath::query() and foreach())
- first
-- subfirst
-- subsecond
-- subthird
- second
-- subfirst
-- subsecond
-- subthird
Piece of code:
$xpath = new \DOMXPath($html);
$catgs = $xpath->query('//*[@id="tree"]/li/a');
foreach ($catgs as $category) {
// first level
echo '- ' . mb_strtolower($category->nodeValue) . '<br>';
// second level
// don't know
}
Thanks in advance!
解决方案 This is what DOMBLAZE is for:
/* DOMBLAZE */ $doc->registerNodeClass("DOMElement","DOMBLAZE"); class DOMBLAZE extends DOMElement{public function __invoke($expression) {return $this->xpath($expression);} function xpath($expression){$result=(new DOMXPath($this->ownerDocument))->evaluate($expression,$this);return($result instanceof DOMNodeList)?new IteratorIterator($result):$result;}}
$list = $doc->getElementById('tree');
foreach ($list('./li') as $item) {
echo '- ', $item('string(./a)'), "\n";
foreach ($item('./ul/li') as $subitem) {
echo '-- ', $subitem('string(./a)'), "\n";
}
}
Output:
- first
-- subfirst
-- subsecond
-- subthird
- second
-- subfirst
-- subsecond
-- subthird
DOMBLAZE is FluentDOM for the poor.
这篇关于使用DOMXPath和函数查询php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文