如何使用 PHP preg_replace 链接 Twitter 用户名?
我想搜索 Twitter 状态对象的文本属性,并将 @username 替换为 @username
。到目前为止我所尝试的看起来像这样:
$pattern = '/([@]{1})([a-zA-Z0-9\_]+)/';
$replace = '<a href="http://twitter.com/\2">\1\2</a>';
$new_string = preg_replace($pattern, $replace, $text);
但它没有做任何替换。我知道我的 reg exp 是错误的,但我无法弄清楚到底在哪里/为什么。帮助?
**编辑:...按要求提供样本数据?
$text = '@janesmith I like that, but my friend @johndoe said it better.';
所需的输出:
@janesmith 我喜欢这个,但是我的朋友@johndoe 说得更好。
***** 我的全部功能 *****
function linkify($string, $twitter=false) {
// reg exp pattern
$pattern = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// convert string URLs to active links
$new_string = preg_replace($pattern, "<a href=\"\\0\">\\0</a>", $string);
if ($twitter) {
$pattern = '/@([a-zA-Z0-9_]+)/';
$replace = '<a href="http://twitter.com/\1">@\1</a>';
$new_string = preg_replace($pattern, $replace, $new_string);
}
return $new_string;
}
I want to search the text property of my twitter status objects and swap out @username for <a href="http:/twitter.com/username">@username</a>
. What I have tried so far looks like this:
$pattern = '/([@]{1})([a-zA-Z0-9\_]+)/';
$replace = '<a href="http://twitter.com/\2">\1\2</a>';
$new_string = preg_replace($pattern, $replace, $text);
But it doesn't do any replacements. I know my reg exp is wrong but I can't figure out exactly where/why. Help?
**Edit: ... sample data as requested?
$text = '@janesmith I like that, but my friend @johndoe said it better.';
Desired output:
@janesmith I like that, but my friend @johndoe said it better.
***** MY FULL FUNCTION *****
function linkify($string, $twitter=false) {
// reg exp pattern
$pattern = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// convert string URLs to active links
$new_string = preg_replace($pattern, "<a href=\"\\0\">\\0</a>", $string);
if ($twitter) {
$pattern = '/@([a-zA-Z0-9_]+)/';
$replace = '<a href="http://twitter.com/\1">@\1</a>';
$new_string = preg_replace($pattern, $replace, $new_string);
}
return $new_string;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么
\
在_
之前?把\
去掉还能用吗?虽然这不应该破坏功能...将
\1
更改为\\1
可能会有所帮助,这样您就可以确定反斜杠已被转义;或更好(自 PHP 4.0.4 起)$1
。但同样,它应该在单引号内按原样工作。此外,您还可以简化:
Why is the
\
there before_
? Does it work if you take out the\
? Though that shouldn't have broken the functionality...It might be helpful to change the
\1
to\\1
so that you're sure the backslash is escaped; or better (since PHP 4.0.4)$1
. But again, it should have worked as-is, within single quotes.Also, you can simplify:
我也更新了一些功能来支持主题标签:
I have update a little the function to support hashtags too:
发现这适用于 URL、@usernames 和 #hashtags:
http://davidwalsh.name/linkify-twitter -饲料
Found this to work for URLs, @usernames, and #hashtags:
http://davidwalsh.name/linkify-twitter-feed
Twitter 更新了他们的 API,至少对我来说,它对用户更加友好。查看 dev.twitter.com/docs/twitter-for-websites 上的文档 单击在这里。您必须首先创建一个应用程序,按照分步说明进行操作,然后创建一个小部件。您在下面看到的小部件显示了我的推文,但您可以创建一个小部件来显示时间轴上的所有操作或搜索结果、列表等...
Twitter has updated their api and, at least to me, it is way more user friendly. Check out the documentation found at the dev.twitter.com/docs/twitter-for-websites click here. You must first creat an app, follow the step-by-step instructions, then you create a widget. The widget you see below displays my tweets, but you can create one that displays all the action on your timeline or the results of searches, lists, etc...