View Single Post
Old 02-27-2008, 11:19 AM   #3 (permalink)
Salathe
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

A nice little start on the subject, and as DeMo said there is always more than one way to skin a cat (as the saying goes).

Using DOM & XPath, we could condense:
PHP Code:
$pos strpos($str"<div id=\"popsearchbd\"");
$pos $pos strlen("<div id=\"popsearchbd\"");
 
if(
$pos == false) {
    echo 
"No information available";
}
else {
 
    while(
1) {
        
$pos strpos($str"fp-buzzmod\">"$pos);
 
        if(
$pos === false) {
            break;
        }
 
        
$pos $pos strlen("fp-buzzmod\">");
        
$temppos $pos;
        
$pos strpos($str"</a>"$pos);
 
        
$datalength $pos $temppos;
 
        
$data substr($str$temppos $datalength);
        echo 
$data;
        echo 
"\n";
    }
 

into:
PHP Code:
$links $xpath->query('//div[@id="popsearchbd"]//a'); 
Now, depending on your skill level or experience with XPath (and/or string functions!) the latter might be even more scary that Sunhil's version! One thing to note is that (at least for the Yahoo site in this tutorial) a User Agent is required, else Yahoo will send back different HTML (not containing the top searches!). However, in the tutorial Sunhil sends along a UA string in the headers so that's ok. :
Salathe is offline  
Reply With Quote