选择多个 class 名称并使用 PHP DOMXpath 在 class 中获取子节点
Choose multiple class name and get childnode inside that class with PHP DOMXpath
<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。我要text node inside <i> tag
。我正在使用 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()
$iTags = $xpath->query('//div[@class="row-0" or @class="row-1"]/span/i');
foreach ($iTags as $iTag) {
var_dump(trim($iTag->nodeValue));
}
我不熟悉 XPath,所以我使用 DOMDocument()
遍历每个 <div>
元素。检查它是否具有值为 row-0 或 row-1 的属性 class。如果是,则获取每个元素 <i>
并转储节点值。
foreach($dom->getElementsByTagName('div') as $div){
if($div->getAttribute('class') == 'row-0' OR $div->getAttribute('class') == 'row-1'){
foreach($div->getElementsByTagName('i') as $i){
var_dump($i->nodeValue);
}
}
}
<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。我要text node inside <i> tag
。我正在使用 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()
$iTags = $xpath->query('//div[@class="row-0" or @class="row-1"]/span/i');
foreach ($iTags as $iTag) {
var_dump(trim($iTag->nodeValue));
}
我不熟悉 XPath,所以我使用 DOMDocument()
遍历每个 <div>
元素。检查它是否具有值为 row-0 或 row-1 的属性 class。如果是,则获取每个元素 <i>
并转储节点值。
foreach($dom->getElementsByTagName('div') as $div){
if($div->getAttribute('class') == 'row-0' OR $div->getAttribute('class') == 'row-1'){
foreach($div->getElementsByTagName('i') as $i){
var_dump($i->nodeValue);
}
}
}