user1295995
user1295995

Reputation: 27

Php file_get_contents() issue

With php file_get_contents() i want just only the post and image. But it's get whole page. (I know there is other way to do this)

Example:

$homepage = file_get_contents('http://www.bdnews24.com/details.php?cid=2&id=221107&hb=5', 
true);
echo $homepage;

It's show full page. Is there any way to show only the post which cid=2&id=221107&hb=5.

Thanks a lot.

Upvotes: 0

Views: 1085

Answers (3)

nachito
nachito

Reputation: 7035

Use PHP's DomDocument to parse the page. You can filter it more if you wish, but this is the general idea.

$url = 'http://www.bdnews24.com/details.php?cid=2&id=221107&hb=5';
// Create new DomDocument
$doc = new DomDocument();
$doc->loadHTMLFile($url);


// Get the post
$post = $doc->getElementById('opage_mid_left');


var_dump($post);

Update: Unless the image is a requirement, I'd use the printer-friendly version: http://www.bdnews24.com/pdetails.php?id=221107, it's much cleaner.

Upvotes: 2

Alex Turpin
Alex Turpin

Reputation: 47776

You will need to parse the resulting HTML using a DOM parser to get the HTML of only the part you want. I like PHP Simple HTML DOM Parser, but as Paul pointed out, PHP also has it's own.

Upvotes: 2

Sujit Agarwal
Sujit Agarwal

Reputation: 12508

you can extract the

<div id="page">
      //POST AND IMAGE EXIST HERE
</div>

part from the fetched contents using regex and push it on your page...

Upvotes: 0

Related Questions