php - Xpath query not working -
here html:
<div class="blogentry expecting featured featured-post-today%e2%80%99s-top-story first"> <h2><a href="/2013/09/03/rachel-zoe-pregnant-expecting-second-child/" rel="bookmark" title="permanent link rachel zoe expecting second child">rachel zoe expecting second child</a></h2> </div> <div class="blogentry expecting featured featured-post-today%e2%80%99s-top-story first"> <h2><a href="someurl" rel="bookmark" title="permanent link rachel zoe expecting second child">sometitle</a></h2> </div>
i'm trying anchor value. here xpath:
$finder->query('//div[@class="blogentry"]//h2//a');
it's returning no value. idea why?
you need use function contains()
here:
$xml = <<<eof <div class="blogentry expecting featured featured-post-today%e2%80%99s-top-story first"> <h2><a href="/2013/09/03/rachel-zoe-pregnant-expecting-second-child/" rel="bookmark" title="permanent link rachel zoe expecting second child">rachel zoe expecting second child</a></h2> </div> eof; $doc = new domdocument(); $doc->loadhtml($xml); $selector = new domxpath($doc); foreach($selector->query('//div[contains(@class,"blogentry")]/h2/a/text()') $item) { echo $item->nodevalue . php_eol; }
output:
rachel zoe expecting second child
Comments
Post a Comment