注意:这与以下问题的不同之处在于,我们在节点内和同一节点的子节点内出现了值:
给出以下html:
$content =
'<html>
<body>
<div>
<p>During the interim there shall be nourishment supplied</p>
</div>
<div>
<p>During the <a href="#">interim</a> there shall be interim nourishment supplied</p>
</div>
<div>
<ul><li>During the interim there shall be nourishment supplied</li></ul>
</div>
</body>
</html>';
以下xpath:
//*[contains(text(),'interim')]
...只提供3场比赛,而我想要四场比赛。根据评论,我期待的四个要素是P P A LI。
答案 0 :(得分:0)
这完全符合预期。请参阅this glot.io链接。
<?php
$html = <<<HTML
<html>
<body>
<div>
<p>During the interim there shall be nourishment supplied</p>
</div>
<div>
<p>During the <a href="#">interim</a> there shall be interim nourishment supplied</p>
</div>
<div>
<ul><li>During the interim there shall be nourishment supplied</li></ul>
</div>
</body>
</html>
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach($xpath->query('//*/text()[contains(.,"interim")]') as $n) var_dump($n->getNodePath());
您将获得四场比赛: