dqqt31923 2014-10-29 04:18
浏览 96
已采纳

DOMXPath / DOMDocument - 在注释块中获取div

Lets say I have this comment block containing HTML:

<html>
<body>

<code class="hidden">
<!-- 
    <div class="a">

        <div class="b">

            <div class="c">
                <a href="link">Link Test 1</a>
            </div>

            <div class="c">
                <a href="link">Link Test 2</a>
            </div>

            <div class="c">
                <a href="link">Link Test 3</a>
            </div>

        </div>

    </div>
-->
</code>

<code>
     <!-- test -->
</code>

</body>
</html>

Using DOMXPath for PHP, how do I get the links and text within the tag?

This is what I have so far:

    $dom = new DOMDocument();
    $dom->loadHTML("HTML STRING"); # not actually in code
    $xpath = new DOMXPath($dom);
    $query = '/html/body/code/comment()';
    $divs = $dom->getElementsByTagName('div')->item(0);

    $entries = $xpath->query($query, $divs);

    foreach($entries as $entry) {

        # shows entire text block
        echo $entry->textContent;

    }

How do I navigate so that I can get the "c" classes and then put the links into an array?

EDIT Please note that there are multiple <code> tags within the page, so I can't just get an element with the code attribute.

  • 写回答

1条回答 默认 最新

  • dongxiezhi0590 2014-10-29 04:26
    关注

    You already can target the comment containing the links, just follow thru that and make another query inside it. Example:

    $sample_markup = '<html>
    <body>
    
    <code class="hidden">
    <!--
        <div class="a">
    
            <div class="b">
    
                <div class="c">
                    <a href="link">Link Test 1</a>
                </div>
    
                <div class="c">
                    <a href="link">Link Test 2</a>
                </div>
    
                <div class="c">
                    <a href="link">Link Test 3</a>
                </div>
    
            </div>
    
        </div>
    -->
    </code>
    
    </body>
    </html>';
    $dom = new DOMDocument();
    $dom->loadHTML($sample_markup); # not actually in code
    $xpath = new DOMXPath($dom);
    $query = '/html/body/code/comment()';
    $entries = $xpath->query($query);
    foreach ($entries as $key => $comment) {
        $value = $comment->nodeValue;
        $html_comment = new DOMDocument();
        $html_comment->loadHTML($value);
        $xpath_sub = new DOMXpath($html_comment);
        $links = $xpath_sub->query('//div[@class="c"]/a'); // target the links!
        // loop each link, do what you have to do
        foreach($links as $link) {
            echo $link->getAttribute('href') . '<br/>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?