【问题标题】:how to fetch text nodes in DOM and PHP?如何在 DOM 和 PHP 中获取文本节点?
【发布时间】:2010-11-30 14:09:06
【问题描述】:

我有以下代码来检索 HTML 文档中的所有超链接

我的问题是如何检索每个锚标记内的文本节点

(即使文本节点是子节点的子节点,例如锚节点有一个跨度节点,该节点有一个文本节点)?

     <?PHP
               $content = "
               <html>
               <head>
               <title>bar , this is an example</title>
               </head>
               <body>
               <a href='aaa'><span>bbb</span></a>
               </body>
               </html>
               ";




        $dom = new DOMDocument();
        @$dom->loadHTML($content);
        $xpath = new DOMXPath($dom);
        $row = $xpath->evaluate("/html/body//a");

        for ($i = 0; $i < $row->length; $i++) {
            $anchor = $row->item($i);
            $href  = $anchor->getAttribute('href');
            // I want the grab the text value which is inside the anchor
            $text = //should have the value "bbb"
        }
       ?>

谢谢

【问题讨论】:

    标签: php html dom


    【解决方案1】:

    $anchor-&gt;textContent

    这里有更多信息DOMNode->textContent

    :D

    【讨论】:

      【解决方案2】:

      这是你可以做的:

      (string)$anchor->nodeValue;
      

      DomDocument::DomNode 页面中引用

      【讨论】:

        猜你喜欢
        • 2011-09-18
        • 1970-01-01
        • 2020-06-21
        • 1970-01-01
        • 2017-09-10
        • 1970-01-01
        • 2011-09-23
        • 1970-01-01
        相关资源
        最近更新 更多