【问题标题】:Twitter "@" and "#" linking in PHP [closed]PHP中的Twitter“@”和“#”链接[关闭]
【发布时间】:2013-04-11 23:45:38
【问题描述】:

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

我发现一些 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,它们可以很好地找到 @#first 瞬间,但不会一直循环。我将如何为它设置一个循环?

$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

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

【问题讨论】:

    标签: php javascript twitter hashtag


    【解决方案1】:

    这里是:

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

    【讨论】:

      【解决方案2】:

      http://php.net/preg_replace 应该可以解决您的问题。

      类似这样的东西(未经测试):

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

      【讨论】:

      • 感谢您抽出宝贵时间回答问题,Patrik。
      • 不客气。除此之外,来自你/@Stephan 的正则表达式比我快速的未经测试的文章要好得多。
      【解决方案3】:

      也许您可以将 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;
      }
      

      【讨论】:

      • 感谢斯蒂芬的帮助。
      【解决方案4】:

      jQuery 脚本使用的是一个简单的正则表达式,用于搜索推文中的 @ 和 #。你可以用 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 还采用第 4 个参数来限制替换的数量,第 5 个参数将包含函数调用后的替换数量。

      【讨论】:

      • 感谢 Silwing 的帮助!
      猜你喜欢
      • 2013-04-08
      • 1970-01-01
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      • 2013-06-08
      • 1970-01-01
      相关资源
      最近更新 更多