Search code examples
phptwitterpreg-replace-callback

Preg_Replace_Callback replacing matched items with Array


I created a replace function to identify twitter accounts and I want to create a link to my internal pages if I have that twitter account present in my data.

//Creates a link for users
    $tester = preg_replace_callback(
    '/\s+@(\w+)/',
      "BrewIdFinder",
    $tester );


function BrewIdFinder($matches){
$result3 = mysql_query("SELECT brew_id FROM places WHERE screen_name = '".$matches."' LIMIT 1");
        $num_rows = mysql_num_rows($result3);
        if($num_rows==0){
        $x= '<a href="http://twitter.com/'.$matches.'" target="_new">@'.$matches.'</a>';
        }else{
        while($row = mysql_fetch_array($result3))
       {
        $brewid= $row['brew_id'];
       }
        $x= '<a href="http://www.brewzinga.com/places/'.$brewid.'" target="_new">@'.$matches.'</a>';
         }
return $x;
}

I get a return for each Twitter Screenname as Array. Can I get some help figuring out what is wrong?


Solution

  • I was able to fix it. I had to specify which match to review. Thanks for the help.

    function BrewIdFinder($matches){
    $sweet= substr($matches[0], 2);
    $result3 = mysql_query("SELECT brew_id FROM places WHERE screen_name like '".$sweet."'");
        $num_rows1 = mysql_num_rows($result3);
        if($num_rows1==0){
        $x= ' <a href="http://twitter.com/'.$sweet.'" target="_new">@'.$sweet.'</a>';
        }else{
        while($row = mysql_fetch_array($result3))
      {
       $brewid= $row['brew_id'];
      }
        $x= ' <a href="http://www.brewzinga.com/places/'.$brewid.'" target="_new">@'.$sweet.'</a>';
             }
    return $x;
    }