使用 @ 符号来识别用户,就像 twitter 一样 [英] Using the @ symbol to identify users like twitter does

查看:33
本文介绍了使用 @ 符号来识别用户,就像 twitter 一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建我自己的 twitter 版本,我不知道如何让我的后端 php 脚本在输入的文本中选择 @membername.包括多个@membername,例如@billy @joseph、@tyrone、@kesha 消息

I'm creating my own version of twitter, I have no idea how to get my back end php script to pick up the @membername within the entered text. Including multiple @membername's for example @billy @joseph, @tyrone,@kesha message

@billy 与 @tyrone 联系,他向 @kesha 询问你欠他的钱.

@billy hit up @tyrone he's bugging @kesha about the money you owe him.

关于如何完成此操作的任何使用脚本?

Any scripts of use on how I can accomplish this?

推荐答案

如何使用正则表达式和 preg_match_all,像这样:

What about using a regex and preg_match_all, like this :

$str = "Including multiple @membername's for example @billy @joseph, @tyrone,@kesha message ";
if (preg_match_all('#(@\w+)#', $str, $m)) {
    var_dump($m[1]);
}


这会给你以下输出:


Which would give you the following output :

array
  0 => string '@membername' (length=11)
  1 => string '@billy' (length=6)
  2 => string '@joseph' (length=7)
  3 => string '@tyrone' (length=7)
  4 => string '@kesha' (length=6)


基本上,在这里,我在正则表达式中使用的模式是匹配的:


Basically, here, the pattern I used in my regex is matching :

  • 一个 @ 字符
  • 任何(多个) 可以在单词中的字符:\w+
    • An @ character
    • Any (more than one) character that can be inside a word : \w+
      • About that, see the Backslash page, in the Regular Expressions (Perl-Compatible) section of the PHP manual

      这篇关于使用 @ 符号来识别用户,就像 twitter 一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆