1

I have this HTML script:

<div class="find-this">I do not need this</div>

<div class="content">
    <div class="find-this">I need this</div>
</div>
<div class="content">
    <div class="find-this">I need this</div>
    <div class="find-this">I need this as well</div>
</div>

So far, I have this:

foreach($html->find('div[class=content]') as $key => $element) :
        $result = $html->find('div[class=find-this]', $key)->innertext;
        echo $result;
endforeach;

How do I find the find-this class that is inside the content class, and not the one above, without knowing how many are inside the needed class and how many are outside? Thank you.

Grigore Dodon
  • 137
  • 3
  • 13

1 Answers1

1

XPath might be what you are looking for. With this code you get only the three nodes that you need.

/* Creates a new DomDocument object */
$dom = new DomDocument;
/* Load the HTML */
$dom->loadHTMLFile("test.html");
/* Create a new XPath object */
$xpath = new DomXPath($dom);
/* Query all <divs> with the class name */
$nodes = $xpath->query("//div[@class='content']//div[@class='find-this']");
/* Set HTTP response header to plain text for debugging output */
header("Content-type: text/plain");
/* Traverse the DOMNodeList object to output each DomNode's nodeValue */
foreach ($nodes as $i => $node) {
    echo "Node($i): ", $node->nodeValue, "\n";
}

Note: I based my answer on this other related answer.

Community
  • 1
  • 1
Pedro Pinheiro
  • 1,059
  • 14
  • 32