简体   繁体   中英

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.

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)

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..)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM