0

我创建了一个替换功能来识别 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 网名作为数组的回报。我可以得到一些帮助来找出问题所在吗?

4

1 回答 1

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;
}
于 2013-08-07T17:17:08.810 回答