【发布时间】: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