1

considering you're searching just one specific value is there to get the only and first result directly without parsing the result with foreach? Like this:

$elements = $dom_xpath->query('//span[@id="loginName"]');


echo $elements->firstNode->nodeValue; // this doesn't exist of course

the html code is

<div>
  <div id="name">text<span id="loginName">Me</span></div>
</div>
hakre
  • 193,403
  • 52
  • 435
  • 836
Sandro Antonucci
  • 1,683
  • 5
  • 29
  • 59

2 Answers2

4

If you need to retrieve the plain-text string inside that <span> element, namely "Me", you can use evaluate instead of query:

$xp->evaluate('string(//span[@id="loginName"])')

This is probably more what you're looking for. It returns:

string(2) "Me"

Probably this is helpful, otherwise, what Jon wrote.

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
3

If you are sure there will be a match, you can use

echo $dom_xpath->query('//span[@id="loginName"]')->item(0)->nodeValue;
Jon
  • 428,835
  • 81
  • 738
  • 806