PHP to parse an HTML page and output a specific div

So, I was trying to embed part of the website into my website due to security reasons, I cannot disclose the website that I am trying to embed, so for this example I am going to use bbc.co. United Kingdom.

Below is the php / html code:

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<body>
<?php
$page = file_get_contents('http://www.bbc.co.uk/');
$doc = new DOMDocument();
$doc->loadHTML($page);
$divs = $doc->getElementsByTagName('div');
foreach($divs as $div) {
    // Loop through the DIVs looking for one withan id of "content"
    // Then echo out its contents (pardon the pun)
    if ($div->getAttribute('id') === 'orb-footer') {
         echo $div->nodeValue;
    }
}
?>  
</body>

<footer>

</footer>
</html>

However, when I load the page, I get a page with a page displaying php code. Can someone tell me where I'm wrong, please.

Thank!

+4
source share
1 answer

This is a general guide to solving PHP problems like this, and it is by no means a direct or specific “bounce” solution for this answer.

, Ctrl + U Chrome .

? , PHP/.

/ ? , PHP-. $doc->loadHtml( htmlentities($page) ).

PHP, XDebug

PHP, , PHP IDE. , .

+2

Source: https://habr.com/ru/post/1669520/


All Articles