0

Note: this differs from the following question in that here we have values appearing within a node and within a childnode of that same node:

XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode

Given the following 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>';

And the following xpath:

//*[contains(text(),'interim')]

... only provides 3 matches, whereas I want four matches. As per comments, the four elements I'm expecting are P P A LI.

Community
  • 1
  • 1
geoidesic
  • 4,649
  • 3
  • 39
  • 59

1 Answers1

0

This works exactly as expected. See this glot.io link.

<?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());

You will get four matches:

  • /html/body/div[1]/p/text()
  • /html/body/div[2]/p/a/text()
  • /html/body/div[2]/p/text()[2]
  • /html/body/div[3]/ul/li/text()
TML
  • 12,813
  • 3
  • 38
  • 45
  • @PaulCrovella Thanks for the feedback - I think someone could probably edit his question to make it more clear, as the answer above was given after extended chat with the original poster on IRC. IOW, the question is deficient in explaining what the user actually wanted, which is why I addressed the reply to "what is expected." – TML Aug 22 '16 at 07:01