I was iterating through a page by DOM object and got stuck at a point.
Here's the sample HTML code i have to iterate through..
...
<div class="some_class">
some Text Some Text
<div class="childDiv">
</div>
<div class="childDiv">
</div>
<div class="childDiv">
</div>
<div class="childDiv">
</div>
</div>
...
Now, here's the partial code..
$dom->loadHTML("content above");
// I want only first level child of this element.
$divs = $dom->childNodes;
foreach ($divs as $div)
{
// here the problem starts - the first node encountered is DomTEXT
// so how am i supposed to skip that and move to the other node.
$childDiv = $div->getElementsByTagName('div');
}
As you can see.. $childNodes
returns DOMNodeList
, then I iterate through it by foreach
, if at anytime a DOMText
is encountered I am unable to skip it.
Please let me know any possible way I can put up a condition differentiating resource type of DOMText
and DOMElement
.
foreach($divs as $div){
if( $div->nodeType !== 1 ) { //Element nodes are of nodeType 1. Text 3. Comments 8. etc rtm
continue;
}
$childDiv = $div->getElementsByTagName('div');
}
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