如何仅显示<;身体></身体>;使用file_get_contents


How do I only display contents within<body></body> using file_get_contents

我有一个代码,它从数据库中获取搜索的整个结果页。我只想抓取并显示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;