I'm pulling images from my Flickr account to my website, and I had used about nine lines of code to create a preg_match_all function that would pull the images.
I've read several times that it is better to parse HTML through DOM.
Personally, I've found it more complicated to parse HTML through DOM. I made up a similar function to pull the images with PHP's DOMDocument, and it's about 22 lines of code. It took awhile to create, and I'm not sure what the benefit was.
The page loads at about the same time for each code, so I'm not sure why I would use DOMDocument.
Does DOMDocument work faster than preg_match_all?
I'll show you my code, if you're interested (you can see how lengthy the DOMDocument code is):
//here's the URL
$flickrGallery = 'http://www.flickr.com/photos/***/collections/***/';
//below is the DOMDocument method
$flickr = new DOMDocument();
$doc->validateOnParse = true;
$flickr->loadHTMLFile($flickrGallery);
$elements = $flickr->getElementById('ViewCollection')->getElementsByTagName('div');
$flickr = array();
for($i=0;$i<$elements->length;$i++){
if($elements->item($i)->hasAttribute('class')&&$elements->item($i)->getAttribute('class')=='setLinkDiv'){
$flickr[] = array(
'href' => $elements->item($i)->getElementsByTagName('a')->item(0)->getAttribute('href'),
'src' => $elements->item($i)->getElementsByTagName('img')->item(0)->getAttribute('src'),
'title' => $elements->item($i)->getElementsByTagName('img')->item(0)->getAttribute('alt')
);
}
}
$elements = NULL;
foreach($flickr as $k=>$v){
$setQuery = explode("/",$flickr[$k]['href']);
$setQuery = $setQuery[4];
echo '<a href="?set='.$setQuery.'"><img src="'.$flickr[$k]['src'].'" title="'.$flickr[$k]['title'].'" width=75 height=75 /></a>';
}
$flickr = NULL;
//preg_match_all code is below
$sets = file_get_contents($flickrGallery);
preg_match_all('/(class="setLink" href="(.*?)".*?class="setThumb" src="(.*?)".*?alt="(.*?)")+/s',$sets,$sets,PREG_SET_ORDER);
foreach($sets as $k=>$v){
$setQuery = explode("/",$sets[$k][2]);
$setQuery = $setQuery[4];
echo '<a href="?set='.$setQuery.'"><img src="'.$sets[$k][3].'" title="'.$sets[$k][4].'" width=75 height=75 /></a>';
}
$sets = NULL;