Simple Dom Helper For Codeigniter
Posted on : 18-06-2009 | In : Codeigniter, PHP
11
Screen scraping with PHP Curl has always been a pain in the arse but Simple Html Dom makes the job a walk in the park.
Being that Simple HTML Dom is a class you would normally create a library fot it, but in this case it fits perfect as a helper. let me show you.
class Welcome extends Controller
{
function __construct()
{
parent::Controller();
$this->load->helper('dom');
}
public function index()
{
// Grab HTML From the URL
$html = file_get_html('http://codeigniter.com/');
// find all link on Codeigniter Site
foreach($html->find('a') as $e)
echo $e->href . '<br>';
}
}
Produces a list like this:

You cant get it much easier.
On the simple dom web site they give you good examples on how to use the parser and within the download they also give extended examples.
You can either download files from there site or download the ci dom helper and a copy of their files here .
Happy Scrapping.


















Hey, nice library, will be helpful..
Will be great if it supports xpath selectors..
Well you are in luck it does ie.
// Find all which attribute id=foo
$ret = $html->find(’div[id=foo]‘);
that is really great!! thanks for sharing
nice helpers.. thanks…
Sweet! Hey I love your tutorials, keep up the good work…
They are good enough to be on binarycake dot com not that I ever want to pay for tutorials… but they are great!
-Brad
Thank you Brad. Glad you like them.
Thanks! This will save me hours!
hey,
just found your site and I must say got some cool stuff here. I just wanted to ask how and where do you save the file so it becomes a helper or library in ci since the site does not have much in the docs about working with ci
It would be best to read the CI user guide. Helpers & Libraries have there own folders.
Great article Lee, and great site on a whole – there are some very interesting screencasts.
On another note – it’s refreshing to hear a brit accent contributing to the community. Your accent sounds very local to me, where abouts are you from?
All the best.
Hi George
Well im from South but now living in the North
.