How to get child element in Zen开发者_StackOverflow中文版d_Dom_Query?
Example HTML:
<h3>
<img src="wow/img.jpg" />
<a href="http://wow.com">wow link</a>
</h3>
How to get href of link through h3 element?
Considering your example only, you could do as follows:
$testHtml = '<h3><img src="wow/img.jpg" /><a href="http://wow.com">wow link</a></h3>';
$dom = new Zend_Dom_Query($testHtml);
// get a element using css child selector
$result = $dom->query('h3 > a');
var_dump($result->current()->getAttribute('href'));
// outputs 'http://wow.com'
In some cases it is reason to use next construction:
$testHtml = '<h3><img src="wow/img.jpg" /><a href="http://wow.com">wow link</a></h3><h3><a href="http://wow2.com">wow link2</a></h3>';
$znd = new Zend_Dom_Query($testHtml);
$result = $znd->query('h3');
if ($item = $result->current()->getElementsByTagName('a')->item(0)) echo $item->getAttribute('href');
echo '<br>';
if ($item = $result->next()->getElementsByTagName('a')->item(0)) echo $item->getAttribute('href');
Print:
http://wow.com
http://wow2.com
精彩评论