如何使用DomDocument获取锚文本? [英] How to get Anchor text using DomDocument?
本文介绍了如何使用DomDocument获取锚文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有这个html:
<a href="http://example.com">Test</a>
我使用以下代码使用dom文档对其进行解析:
I parse it using dom document with this code:
$dom = new DomDocument();
@$dom->loadHTML($html);
$urls = $dom->getElementsByTagName('a');
然后运行以下代码:
foreach ($urls as $url)
{
//echo "<br> {$url->getAttribute('href')} , {$url->getAttribute('title')}";
foreach ($url->attributes as $a)
{
echo "<br>$a->name is $a->value";
}
echo "<hr><br>";
}
执行此操作时,我仅将'href'作为url的属性,无法获取'anchor text'(在上述情况下为'Test').如何获取链接的锚文本?
When I do this, I only see 'href' as an attribute of the url, there's no way to get the 'anchor text' (in the above case 'Test'). How can I get the anchor text of the link?
推荐答案
foreach ($urls as $url) {
$attributes = $url->attributes;
echo "<br>$url->nodeValue is $attributes->href";
}
这篇关于如何使用DomDocument获取锚文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文