<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>
This is the HTML that i want to parse and get contents. I want text node inside <i> tag
. I am using DOMDocument
and 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?
You can do all that with the following XPath query:
//*[starts-with(@class,"row-")]/span/i/text()
MDN on starts-with:
The
starts-with
checks whether the first string starts with the second string and returns true or false.
If you are interested in all text nodes in these rows, so also the ones in the b
tags, and any other tags that might be in those rows, then use the double slash:
//*[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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With