选择多个类名称,并使用PHP DOMXpath在该类内获取childnode
作者:互联网
<div id="conti">
<div class="no_matter"></div>
<div class="row-0">
<b></b>
<span>
<i>"child node that i want to get"</i>
</span>
</div>
<div class="row-1">
<b></b>
<span>
<i>"child node that i want to get"</i>
</span>
</div>
<div class="row-0">
<b></b>
<span>
<i>"child node that i want to get"</i>
</span>
</div>
<div class="row-1">
<b></b>
<span>
<i>"child node that i want to get"</i>
</span>
</div>
...
...
class row-0 and row-1 repeats itself
...
...
</div>
这是我要解析并获取内容的HTML.我想要< i>内的文本节点标签.我正在使用DOMDocument和DOMXpath
$dom = new DOMDocument();
$dom->loadHTMLFile('http://www.meal.org/anter.php');
$dom->preserveWhiteSpace = true;
$xpath = new DOMXPath($dom);
$row = $xpath->query('//*[@class="row-0" ]'); //my problem begins there. I want both 'row-0' and 'row-1'. How i am gonna choose multiple class?
//and than how i am gonna get `<i>` tag inside every `row-0` and `row-1` class and get the text node?
解决方法:
您可以使用以下XPath查询完成所有操作:
//*[starts-with(@class,"row-")]/span/i/text()
The
starts-with
checks whether the first string starts with the second string and returns true or false.
如果您对这些行中的所有文本节点,b标记中的文本节点以及这些行中可能存在的任何其他标记感兴趣,请使用双斜杠:
//*[starts-with(@class,"row-")]//text()
标签:domdocument,domxpath,php 来源: https://codeday.me/bug/20191027/1943122.html