我在PHP中创建一个Twitter结果列表,我已经使用PHP将字符串中的文本链接转换为可点击链接而不是纯文本.
在某些Twitter结果中,字符串包含哈希标记,只是前缀为#字符的文本.
我想做的是拿hastag并将其转换为可点击的链接.
例如:
Hello my name is Dan, and im working with the #twitter api
Run Code Online (Sandbox Code Playgroud)
我想转变成:
Hello my name is Dan, and im working with the <a href="http://search.twitter.com/search?q=%23twitter">#twitter</a> api
Run Code Online (Sandbox Code Playgroud)
请注意,在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"]);
Run Code Online (Sandbox Code Playgroud)
Tim*_*per 10
$str = preg_replace('/\#([a-z0-9]+)/i', '<a href="http://search.twitter.com/search?q=%23$1">#$1</a>', $str);
Run Code Online (Sandbox Code Playgroud)
编辑
就两种不同的模式而言,只需:
$str = preg_replace(array($url_pattern, $hash_pattern), array($url_replacement, $hash_replacement), $str);
Run Code Online (Sandbox Code Playgroud)