How to get Anchor text using DomDocument?

前端 未结 4 1620
深忆病人
深忆病人 2020-12-10 07:46

Say I have this html:

Test

I parse it using dom document with this code:

$do         


        
相关标签:
4条回答
  • 2020-12-10 07:54
    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';   
    }
    
    0 讨论(0)
  • 2020-12-10 08:09

    Use DOMNode::$nodeValue:

    echo $url->nodeValue;
    
    0 讨论(0)
  • 2020-12-10 08:17
    foreach ($urls as $url) {
        $attributes = $url->attributes;
        echo "<br>$url->nodeValue is $attributes->href";
    } 
    
    0 讨论(0)
  • 2020-12-10 08:18

    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?

    0 讨论(0)
提交回复
热议问题