$html = 'foo blah <a href="">foo bar</a&开发者_JS百科gt; blah <a href="">foo</a> blah foo';
$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//text()');
echo get_class($nodes);
foreach($nodes as $node) {
echo '<br />';
echo get_class($node) . ':'; //look here
echo $node->wholeText;
}
Why is the class type DOMText for each $node ?
Your query is asking for text nodes with //text()
. :)
DOMText is a specialized DOMNode, which in its turn is the base object for just about every DOM related object (except DOMXPath, I believe, which makes sense)
The object hierarchy for DOMText is actually as followed;
DomNode
- DOMCharacterData
- DomText
You can learn more about this by examining the docs
精彩评论