我有这个HTML脚本:
<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>
到目前为止,我有这个:
foreach($html->find('div[class=content]') as $key => $element) :
$result = $html->find('div[class=find-this]', $key)->innertext;
echo $result;
endforeach;
如何找到find-this
类中的content
类,而不是上面的类,而不知道所需类中有多少类,外面有多少类?谢谢。
答案 0 :(得分:1)
XPath可能就是你要找的东西。使用此代码,您只能获得所需的三个节点。
/* 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";
}
注意:我的答案基于this other related answer。