Search code examples
phpparsingdomdocumentdomxpath

DOMXpath remove tag


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!


Solution

  • 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'