add matches from preg_match_all - into array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ctech
Forum Newbie
Posts: 4
Joined: Wed Jun 25, 2008 1:54 pm

add matches from preg_match_all - into array

Post by Ctech »

Hi,

I got a preg_match_all from a website.

it picks up 3 variables: domain, keyword, ranking.

However sometimes it finds the same domain more than ones on the same keyword, but a different ranking. How can I add only the match with the highest ranking to the array?

------------------
here is my code:


Code: Select all

<?php
 
ini_set('user_agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5) Gecko/2008050509 Firefox/3.0b5');
 
 
 
// Add all domanes to an array
$domane = array("amedisin.no","startsiden.no");
$keyword = array("soneterapi+oslo","akupunktur+oslo");
 
$pages = array("","&start=10","&start=20");
 
 
        
foreach ($keyword as $v) {  
        
        foreach ($pages as $val) {
        
        
        $file = file_get_contents('http://www.google.no/search?hl=no&q='.$v.''.$val.'&sa=N');
        
        preg_match_all("/<h2 class=r><a href=\"http?:\/\/www\.(.*?)\/.*?'res','(.*?)'/",$file,$matches);
        echo "<pre>";
        array_shift($matches);
        print_r($matches);
        
        
        echo $val."<br />";
        
                foreach ($domane as $value) { // For each domane in array look for ranking
                
                    // Finds a specific domane in the list
                    // Check if it returns a result
                    if (array_search($value, $matches[0]) === false) {
                    echo "Don't exisit in array"; 
                    $noresult = "noresult";
                    }
                    
                    else { // check the ranking of the result found
                    $key = array_search($value, $matches[0]);
                    
                    // Echo result
                    echo "Domane: ".$value."<br />";
                    echo "Keyword: ".$v."<br />";
                    echo "Ranking: ".$matches[1][$key];
                    
                    // Adding a sleep break
                    $sleep = rand(1,4);
                    sleep($sleep);
                    echo "<br />Sleep for: ".$sleep."";
                    
                    // sum up time found to match with number of domanes
                    $found++;
                    echo "<br />Found total: ".$found."<br /><br />";
                    
                    
                    // Here I add the findings to mySQL
                    // Taken away the code
                    // However here I need to filter out if there already is a finding with a higher ranking just added, while this script was running. 
                    
                    
                    }
                }
        
        if (!$noresult == "noresult") { break;}
        }
 
}
 
?>
Post Reply