简体   繁体   中英

How to get Anchor text using DOMDocument?

Say I have this html:

<a href="http://example.com">Test</a>

I parse it using DOMDocument with this code:

$dom = new DomDocument();
@$dom->loadHTML($html);
$urls = $dom->getElementsByTagName('a');

And then I run this code:

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>";
}

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";
} 

使用DOMNode::$nodeValue

echo $url->nodeValue;
here is two line code may it help some one

$html   =   file_get_html($link);
foreach($html->find("a") as $key=>$val)
{
  echo $val->src;
  echo '\n';   
}

The text "Test" is actually a DOM Text node so you can fetch the content by going through the children nodes of $url.

You can check this post for solution: How to get innerHTML of DOMNode?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM