PHP Twitter 用真实链接替换链接和主题标签 [英] PHP Twitter replace link and hashtag with real link
本文介绍了PHP Twitter 用真实链接替换链接和主题标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在遍历来自 Twitter API 的 JSON 响应.每个 API 响应都会给我一条类似于以下内容的推文:
I am looping over JSON response from Twitter API. Each API response gives me a tweet similar to:
我的名字是@john,我喜欢#soccer,请访问我
Hi my name is @john, and I love #soccer, visit me
我正在尝试替换 @john
,并插入 <a href=http://twitter.com/john>@john</a>
但@john
后面的逗号 (,
) 是问题所在.
I am trying to replace @john
, and insert <a href=http://twitter.com/john>@john</a>
but the comma (,
) after @john
, is the problem.
如何替换标签前后的点、逗号等?
How to replace dots, commas, etc before and after the tag?
推荐答案
$str = preg_replace("/@(\w+)/i", "<a href=\"http://twitter.com/$1\">$0</a>", $str);
这篇关于PHP Twitter 用真实链接替换链接和主题标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文