Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to differentiate between a domText and domElement object?

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.

like image 460
Sarthak Sawhney Avatar asked Jul 04 '12 13:07

Sarthak Sawhney


1 Answers

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');
}
like image 169
Esailija Avatar answered Oct 18 '22 09:10

Esailija