Manos Serifios
Manos Serifios

Reputation: 577

DOMXpath can't get inside text

I have an HTML file like this above:

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

I want to get the "Hello" string
I tried this $res = $xpath->query("//div[@class='lvlone']/div[@class='lvltwo']/span"); but nothing!
Any ideas?
Thanks anyway.

Upvotes: 0

Views: 351

Answers (2)

Svetoslav
Svetoslav

Reputation: 4676

If you want to parse how HTML page I suggest you to use this: http://simplehtmldom.sourceforge.net

Simplehtmldom is PHP class which parse how web page content and gives you ability to search and find by defferent parameters.. (tags, class, id, etc..)

Upvotes: 0

air4x
air4x

Reputation: 5683

It works. try this

$dom = new DOMDocument;
@$dom->loadHTML('<div class = "lvlone">
    <div class = "lvltwo">
        <span>Hello</span>
    </div>
</div>');
$x = new DOMXPath($dom);
$entries = $x->query("//div[@class='lvlone']/div[@class='lvltwo']/span");
foreach ($entries as $entry)
  var_dump($entry->nodeValue);

 //Output - string 'Hello' (length=5)

Upvotes: 1

Related Questions