如何仅在使用file_get_contents中<body></body>显示内容



我有一个代码,它从数据库中获取搜索的整个结果页。我只想抓取并显示body标记中的内容,这样我就可以自己操作结果页面的其余部分。请告知。谢谢

您可以使用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;

最新更新