【问题标题】:Turn a twitter hash tag into a link and links to clickable links将 twitter 哈希标签转换为链接和指向可点击链接的链接
【发布时间】:2010-09-03 19:52:39
【问题描述】:

我正在用 PHP 创建 Twitter 结果列表,我已经在使用 PHP 将返回的字符串中的文本链接转换为可点击的链接,而不是纯文本。

在某些 Twitter 结果中,字符串包含井号标签,只是前面带有 # 字符的文本。

我想做的是获取hastag并将其转换为可点击的链接。

例如:

Hello my name is Dan, and im working with the #twitter api

我想变成什么:

Hello my name is Dan, and im working with the <a href="http://search.twitter.com/search?q=%23twitter">#twitter</a> api

请注意,在 URL 中我必须将 # 更改为 %23 - 我认为是编码或解码。

还有……

我已经在使用下面的代码在字符串中创建可点击的链接,是否可以将两个 RegEx 结合起来?

这是我将链接转换为可点击链接的 PHP:

            //Convert links in string to links
            preg_match('/(http:\/\/[^\s]+)/', $row["content"], $text);
            $hypertext = "<a target='_blank' href=\"". $text[0] . "\">" . $text[0] . "</a>";
            $newString = preg_replace('/(http:\/\/[^\s]+)/', $hypertext, $row["content"]);

【问题讨论】:

    标签: php


    【解决方案1】:
    $str = preg_replace('/\#([a-z0-9]+)/i', '<a href="http://search.twitter.com/search?q=%23$1">#$1</a>', $str);
    

    编辑

    就两种不同的模式而言,只需这样做:

    $str = preg_replace(array($url_pattern, $hash_pattern), array($url_replacement, $hash_replacement), $str);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 2011-04-17
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      • 2022-11-24
      • 2018-09-29
      相关资源
      最近更新 更多