Preg_Replace_Callback用Array替换匹配的项目

时间:2013-07-31 03:13:05

标签: php twitter preg-replace-callback

我创建了一个替换函数来识别Twitter帐户,如果我的数据中有Twitter帐户,我想创建一个指向我内部页面的链接。

//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;
}

我获得每个Twitter Screenname作为Array的回报。我可以帮助找出问题所在吗?

1 个答案:

答案 0 :(得分:1)

我能够解决它。我必须指定要审核的匹配项。谢谢你的帮助。

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;
}