Twitter "@" and "#" linking in PHP

Twitter "@" and "#" linking in PHP

本文关键字:quot in PHP linking Twitter and      更新时间:2023-09-26

我试图转换所有@#链接时,将代码放入我的网站。

我发现了一些Jquery代码,看起来使用正则表达式搜索@#,但我不确定如何使用PHP完成同样的事情。

代码是:

  at: function(tweet) {
    return tweet.replace(/'B[@@]([a-zA-Z0-9_]{1,20})/g, function(m, username) {
      return '<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=' + username + '">@' + username + '</a>';
    });
  },
  hash: function(tweet) {
    return tweet.replace(/(^|'s+)#('w+)/gi, function(m, before, hash) {
      return before + '<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' + hash + '">#' + hash + '</a>';
    });
  },

如果我有这样的东西:

@BobBarker is going to #blahblah and also @BillyBob

我需要搜索并执行以下操作:

<a target="_blank" 
   class="twtr-atreply" 
   href="http://twitter.com/intent/user?screen_name=BobBarker'">@BobBarker
</a>
<a target="_blank" 
   class="twtr-atreply" 
   href="http://twitter.com/intent/user?screen_name=BillyBob'">@BillyBob
</a>

对于散列标签也是如此:

#blahblah

我需要搜索并执行以下操作:

<a target="_blank" 
   class="twtr-hashtag" 
   href="http://twitter.com/search?q=%23blahblah">#blahblah
</a>

任何帮助将是伟大的!

我把一些PHP放在一起,可以很好地查找第一个瞬间@#,但不会继续循环。我该如何为它建立一个循环?
$theTweet = "@BobBarker is going to #blahblah and also @BillyBob";
if (preg_match("/'B[@@]([a-zA-Z0-9_]{1,20})/", $theTweet, $matches)) {
    $theTweet = str_replace($matches[0],'<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=' . trim($matches[0]) . '"> ' . trim($matches[0]) . '</a> ',$theTweet);
    echo $theTweet . '<br />';
}
if (preg_match("/(^|'s+)#('w+)/", $theTweet, $matches)) {
    $theTweet = str_replace($matches[0],'<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' . trim($matches[0]) . '"> ' . trim($matches[0]) . '</a> ',$theTweet);
    echo $theTweet;
}

更新# 2

回答了我自己的问题:0)

如下:

$theTweet = "@BobBarker is going to #blahblah and also @BillyBob";
    preg_match_all("@(https?://([-'w'.]+)+(:'d+)?(/(['w/_'.]*('?'S+)?)?)?)@", $theTweet, $matches); //Take care of the http's
    foreach($matches[0] as $tweetsHTTP){
        $theTweet = str_replace($tweetsHTTP,'<a target="_blank" class="js-display-url" href="' . $tweetsHTTP . '"> ' . $tweetsHTTP . '</a> ', $theTweet);
    }
    preg_match_all("/'B[@@]([a-zA-Z0-9_]{1,20})/", $theTweet, $matches); //Take care of the @'s
    foreach($matches[0] as $tweetsAT){
        $theTweet = str_replace($tweetsAT,'<a target="_blank" class="twtr-atreply" href="http://twitter.com/' . str_replace("@", "", $tweetsAT) . '"> ' . $tweetsAT . '</a> ', $theTweet);
    }
    preg_match_all("/(^|'s+)#('w+)/", $theTweet, $matches); //Take care of the #'s
    foreach($matches[0] as $tweetsHash){
        $theTweet = str_replace($tweetsHash,'<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23' . $tweetsHash . '&src=hash"> ' . $tweetsHash . '</a> ', $theTweet);
    }

http://php.net/preg_replace应该能解决你的问题。

像这样(未测试):

preg_replace("/ @(['w'd]+) /i", "<a href='"http://twitter.com/$1/'">@$1</a>", $tweet);

也许您可以将jQuery的相同行为包装到php函数中。大概是这样的。

function writeLinks($tweet){
    //for mentions
    $tweet = preg_replace("/'B[@]([a-zA-Z0-9_]{1,20})/i", '<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name'=$1">$1</a>',$tweet);
    //for hashtags
    $tweet = preg_replace("/(^|'s+)#('w+)/i", '<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search'?q=%23$1>#$1</a>', $tweet);
    return $tweet;
}

jQuery脚本使用的是一个简单的正则表达式,在tweet中搜索@和#。您可以使用PHP中的正则表达式函数做类似的事情。

preg_replace函数的作用与示例中的javascript函数大致相同。

function tweetReplaceAt($tweet) {
  return preg_replace("/'B[@@]([a-zA-Z0-9_]{1,20})/",'<a target="_blank" class="twtr-atreply" href="http://twitter.com/intent/user?screen_name=$1">@$1</a>',$tweet);
}
function tweetReplaceHash($tweet) {
  return preg_replace("/(^|'s+)#('w+)/i",'<a target="_blank" class="twtr-hashtag" href="http://twitter.com/search?q=%23$1">#$1</a>',$atreplacedTweet);
}

然后你可以在你的推特上使用这些功能。

preg_replace还接受第四个参数来限制替换的数量,第五个参数将包含函数调用后的替换数量。