7

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?

Syscall
  • 19,327
  • 10
  • 37
  • 52
Ali
  • 261,656
  • 265
  • 575
  • 769

4 Answers4

6
foreach ($urls as $url) {
    $attributes = $url->attributes;
    echo "<br>$url->nodeValue is $attributes->href";
} 
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
  • i have a image tag as the anchor text of a link. if i use nodeValue it does not return anything. i am using find('a[href=url]') to get the matching anchor link. How can i get the image tag?? – peter Jul 04 '11 at 07:42
  • textContent does not return anything. it should return something like something but does not – peter Jul 04 '11 at 09:57
  • innertext did the trick. it returns the image tag fine now. thanks dqhendricks – peter Jul 04 '11 at 10:28
5

Use DOMNode::$nodeValue:

echo $url->nodeValue;
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
1
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';   
}
Khandad Niazi
  • 2,326
  • 3
  • 25
  • 22
0

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?

Community
  • 1
  • 1
Christian Joudrey
  • 3,441
  • 25
  • 25
  • This is actually meant to obtain the innerHTML, you might want to use nodeValue if all you want is the text inside as @lonesomeday pointed out. – Christian Joudrey Jan 07 '11 at 18:49