【问题标题】:Echo all links in a HTML page PHP [closed]回显HTML页面PHP中的所有链接[关闭]
【发布时间】:2015-10-10 22:24:14
【问题描述】:

如何使用 DOMDocument 回显 HTML 页面中的所有链接? 我当前的代码

$html = file_get_contents("http://en.wikipedia.org");
$dom = new DOMDocument;
$dom->loadHTML($html);
$output = new DOMDocument;
$links = $dom ->getElementsByTagName('a');
foreach($links as $link)
{
     $output->importNode($link);
}
echo $output -> saveHTML();

【问题讨论】:

    标签: php html dom


    【解决方案1】:

    这里是:

    <?php
    $html = file_get_contents("http://en.wikipedia.org");
    $dom = new DOMDocument;
    $dom->loadHTML($html);
    $output = new DOMDocument;
    $output->formatOutput = true;
    $links = $dom ->getElementsByTagName('a');
    foreach($links as $link){
        $node = $output->importNode($link, true);
        $output->appendChild($node);
    }
    echo $output -> saveHTML();
    

    【讨论】:

    • 这对我有用,谢谢。
    猜你喜欢
    • 2014-01-21
    • 2014-12-19
    • 2018-07-13
    • 1970-01-01
    • 2021-07-16
    • 2014-10-20
    • 1970-01-01
    • 2019-07-13
    • 2018-02-13
    相关资源
    最近更新 更多