Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a code that is grabbing the entire results page of a search from a database. I want to only grab and display the contents within the body tags so that I can manipulate the rest of the results page myself. Please advise. Thanks.

like image 784
Point Blank Avatar asked Oct 24 '25 06:10

Point Blank


1 Answers

You can use DOMDocument to extract the parts of the page that you want.

$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;
like image 98
Bad Wolf Avatar answered Oct 26 '25 19:10

Bad Wolf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!