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

Poi*_*ank 2 php file-get-contents

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

Bad*_*olf 7

您可以使用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;
Run Code Online (Sandbox Code Playgroud)