phphtmlsimpledom

get nested element content with simpleDOM


How do I get ext_link content with simple DOM from the following DOM?

...
<td class="player">
<img src="Web/Images/Players/33/45d5652.png" />
<a class="ext_link" target="_blank" href="2017-18/Player/7124151">Michel Jordan</a>
</td>
... 

I tried with:

foreach($html->find('td.player')->find('a.ext_link') as $element) {
       echo $element->innertext . '<br>';
}

and also with:

foreach($html->find('td.player')->children(2) as $element) {
       echo $element->innertext . '<br>';
}

But both attempts didn't work.

Note that I can't do $html->find('a.ext_link') because there are other a elements with ext_link class in the document. I only need the ones inside the td with class player


Solution

  • I think that you can find all the (nested) children like this:

    foreach($html->find('td[class=player] a[class="ext_link"]') as $element){
        echo $element->innertext();
    }
    

    If you only want the direct children, this can be an option:

    foreach($html->find('td[class=player]') as $element){
        foreach ($element->childNodes() as $childNode) {
            if ($childNode->nodeName() === "a") {
                echo $childNode->innertext();
            }
        }
    }