Sarthak Sawhney
Sarthak Sawhney

Reputation: 432

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.

Upvotes: 5

Views: 4006

Answers (1)

Esailija
Esailija

Reputation: 140236

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');
}

Upvotes: 10

Related Questions