【问题标题】:How do I only display contents within<body></body> using file_get_contents如何使用 file_get_contents 仅显示 <body></body> 内的内容
【发布时间】:2013-08-06 21:02:21
【问题描述】:

我有一个从数据库中抓取整个搜索结果页面的代码。我只想抓取并显示正文标签中的内容,以便我可以自己操作结果页面的其余部分。请指教。谢谢。

【问题讨论】:

    标签: php file-get-contents


    【解决方案1】:

    您可以使用DOMDocument 提取您想要的页面部分。

    $html = file_get_contents("some resource");
    libxml_use_internal_errors(true); //Prevents Warnings, remove if desired
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $body = "";
    foreach($dom->getElementsByTagName("body")->item(0)->childNodes as $child) {
        $body .= $dom->saveHTML($child);
    }
    echo $body;
    

    【讨论】:

      猜你喜欢
      • 2015-05-04
      • 2011-09-05
      • 2023-03-06
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多