如何使用 PHP preg_replace 链接 Twitter 用户名?

发布于 2024-09-29 04:10:30 字数 1274 浏览 1 评论 0原文

我想搜索 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

救赎№ 2024-10-06 04:10:30

为什么\_之前?把\去掉还能用吗?虽然这不应该破坏功能...

\1 更改为 \\1 可能会有所帮助,这样您就可以确定反斜杠已被转义;或更好(自 PHP 4.0.4 起)$1。但同样,它应该在单引号内按原样工作。

此外,您还可以简化:

$pattern = '/@([a-zA-Z0-9_]+)/';
$replace = '<a href="http://twitter.com/$1">@$1</a>';

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:

$pattern = '/@([a-zA-Z0-9_]+)/';
$replace = '<a href="http://twitter.com/$1">@$1</a>';
向日葵 2024-10-06 04:10:30

我也更新了一些功能来支持主题标签:

函数 linkify($string, $twitter=false) {

// reg exp 模式

$pattern = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9-.]+.[a-zA-Z]{2,3}(\ /\S*)?/";

// 将字符串 URL 转换为活动链接
$new_string = preg_replace($pattern, "\0", $string);

如果($twitter){

 $pattern = '/\@([a-zA-Z0-9_]+)/';

   $replace = '@\1';

   $new_string = preg_replace($pattern, $replace, $new_string);


   $pattern = '/\#([a-zA-Z0-9_]+)/';

   $replace = '#\1';

   $new_string = preg_replace($pattern, $replace, $new_string);

}

返回$new_string;
}

I have update a little the function to support hashtags too:

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, "\0", $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);


   $pattern = '/\#([a-zA-Z0-9_]+)/';

   $replace = '<a href="http://twitter.com/search/#\1">#\1</a>';

   $new_string = preg_replace($pattern, $replace, $new_string);

}

return $new_string;
}

葬シ愛 2024-10-06 04:10:30

发现这适用于 URL、@usernames 和 #hashtags:

http://davidwalsh.name/linkify-twitter -饲料

Found this to work for URLs, @usernames, and #hashtags:

http://davidwalsh.name/linkify-twitter-feed

筱果果 2024-10-06 04:10:30

Twitter 更新了他们的 API,至少对我来说,它对用户更加友好。查看 dev.twitter.com/docs/twitter-for-websites 上的文档 单击在这里。您必须首先创建一个应用程序,按照分步说明进行操作,然后创建一个小部件。您在下面看到的小部件显示了我的推文,但您可以创建一个小部件来显示时间轴上的所有操作或搜索结果、列表等...

     <a class="twitter-timeline" ref="https://twitter.com/SKAmerica" data-widget-id="359949405157203970">Tweets by @SKAmerica</a>
     <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id))js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

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...

     <a class="twitter-timeline" ref="https://twitter.com/SKAmerica" data-widget-id="359949405157203970">Tweets by @SKAmerica</a>
     <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id))js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文