3

I have this HTML code:

<div class ="lvlone">
    <div class = "lvltwo"> Hello
        <span>World</span>
    </div>
</div>

I do this:$res = $xpath->query(//div[@class='lvlone']/div[@class='lvltwo']);

I get Hello World including the string in <span> tag but i down want it!
I only want the Hello.
What can i do ?
Thanks!

Manos Serifios
  • 577
  • 2
  • 7
  • 22
  • You're probably going to need to use `text()` at some point to extract only text nodes and not other children. I'd start looking there. – TheZ Oct 31 '12 at 23:08
  • thanks man! I am new at DOMDocument and i didnt find this at PHP.net! – Manos Serifios Oct 31 '12 at 23:22
  • No problem, I remember learning XPath... oh wait no, I'm _still_ learning my way around XPath! Haha. – TheZ Oct 31 '12 at 23:29

1 Answers1

1

As TheZ points out, you can use the text() function from XPath:

$nodes = $xpath->query( '//div[@class="lvltwo"]/text()');
echo $nodes->item(0)->nodeValue; // Prints 'Hello'
nickb
  • 59,313
  • 13
  • 108
  • 143