问题描述
限时送ChatGPT账号..我正在创建我自己的 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+
关于此,请参阅 反斜杠 页面,在PHP 手册的正则表达式(Perl 兼容) 部分
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屋!
更多推荐
[db:关键词]
发布评论