views:

35

answers:

2

I have the following code in PHP

          $ch = curl_init("http://blog.com");
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          $output = curl_exec($ch);      
          curl_close($ch);
          echo $output;

I am trying to import the block of code in between <div id="content"> and </div> I would like to know the best way of extracting this code.

Thank you!

+2  A: 

DOM would be the best way. Here's a detailed documentation: http://php.net/manual/en/book.dom.php

Ruel
A: 

Once you get the raw HTML, you can use PHP Simple HTML DOM Parser to extract fragments of HTML and/or text content from the HTML document. PHP Simple HTML DOM Parser lets you use jQuery like selectors to traverse HTML. For example:

$html = new simple_html_dom();
// following line requires some PHP.ini settings enabled on your server
// you can otherwise fallback to CURL and use $html->load($output); instead
$html->load_file('http://blog.com');
$content= $html->find('#content');
echo $content->plaintext;
Salman A