3

Possible Duplicate:
read XML tag id from php

what is the way to fetch data from A Specific div id Using PHP. What i want to do is to fetch data from a div id called <div id="content"> , so all the data from that div id will be fetched in a variable.
I can fetch all content with my script but cant filter it to fetch data from a Specific div tag.
Here is the script i am using to fetch any content:

function file_get_contents_curl($url)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}
$html = file_get_contents_curl("http://www.example.com");
//parsing all content:
$doc = new DOMDocument();
@$doc->loadHTML($html);
echo "$html";


any idea?

Community
  • 1
  • 1
user1642787
  • 75
  • 1
  • 2
  • 7
  • you dont need curl to fetch the content. there is `DOMDocument::loadHTMLFile`. You can fetch an element by ID either via `getElementById()` or via XPath `//element[@id="name"]`. This has been asked and answered multiple times before and should be easy to find. When we ask you to do research in http://stackoverflow.com/questions/ask-advice we really mean it. – Gordon Sep 03 '12 at 11:20

2 Answers2

4

Try this but make sure you have downloaded and included PHP Simple HTML DOM Parser

$html = file_get_html("http://www.example.com");
$displaybody = $html->find('div[id=content]', 0)->plaintext;

Their are some ways to exclude content from div id or Tag id like,

1) Using regex

2) Using SimpleXML

3) With DOM extension Or XPath

Community
  • 1
  • 1
Vinay
  • 2,564
  • 4
  • 26
  • 35
  • thanks Vinay its works like a magic! one more question is there any way to exclude content from a div id.. thanks – user1642787 Sep 03 '12 at 12:12
  • @user1642787 I have updated answer with some of way I know to exclude content from a div id. – Vinay Sep 03 '12 at 12:51
  • thanks, i have tried with this but dont works `$regex = '#\
    (.+?)\<\/div\>#s'; preg_match($regex, $displaybody, $matches); $match = $matches[0]; echo $match;` any idea?
    – user1642787 Sep 03 '12 at 13:01
  • Read documentation I have posted, and then try, Now use this, $regex = '#\
    (.+?)\<\/div\>#s'; preg_match($regex, $html, $matches); $match = $matches[0]; echo $match;
    – Vinay Sep 03 '12 at 13:06
  • i am sorry that was a div class i wanna exclude , it is that div class `
    ` i have read all your documentation ..applied.. but not have the result .. any idea?
    – user1642787 Sep 03 '12 at 13:09
  • replace $displaybody with $html from your second comment. and try. – Vinay Sep 03 '12 at 13:14
  • i have updated this script so it is now $displaybody – user1642787 Sep 03 '12 at 13:18
0

With Simple HTML DOM Parser

<?php

$content = file_get_contents_curl("http://www.example.com");

$html = file_get_html($content );
$ret = $html->find('div[id=divname]');
?>
Praveen kalal
  • 2,148
  • 4
  • 19
  • 33