Brian Ecker
Brian Ecker

Reputation: 2077

Use curl to get a specific div from a web page

I have a page being generated by from an rss feed, but it is being generated with additional html, head, and body tags. I would like to get only one div and it's contents from that page and load it into my page. I figured I would need to use curl to load from the page and I found many examples of loading the entire page, but how can I load only the specific div I am looking for?

Upvotes: 0

Views: 2689

Answers (1)

Niels Keurentjes
Niels Keurentjes

Reputation: 41958

First of all, no reason to use curl for a simple page download, just use:

$html = file_get_contents('http://www.stackoverflow.com');

After that you have 2 options:

  1. Use a DOM Parser to parse the HTML into a DOM tree, and use it to retrieve the specific element. This only works well if the page is relatively valid HTML.
  2. Use a regexp or strpos and substr to find the specific part of the code you care about.

Purists usually hate on option 2, because it is inherently inferior to the first on a lot of aspects. It is however also much less work to code and for bad source code most probably more reliable. Which option you want to use is up to you - for a simple one-off case I'd definitely go for the simple approach and use a regexp if you know them, or strpos if you don't.

Upvotes: 2

Related Questions