php - Xpath query not working
Get the solution ↓↓↓Here is the 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 to 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 to Rachel Zoe Expecting Second Child">sometitle</a></h2>
</div>
I'm trying to get the anchor value. Here is my XPATH:
$finder->query('//div[@class="blogEntry"]//h2//a');
It's returning no value. Any idea why?
Answer
Solution:
You need to use the function 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 to 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()') as $item) {
echo $item->nodeValue . PHP_EOL;
}
Output:
Rachel Zoe Expecting SecondВ Child
Answer
Solution:
Okay!!
Here is what you are looking for :-
//div[contains(@class,"blogEntry")]/h2/a/text()
output
Rachel Zoe Expecting SecondВ Child
sometitle
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: warning: undefined array key
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.