检查PHP中的“@”字符后面的单词 [英] Check the word after a '@' character in PHP
本文介绍了检查PHP中的“@”字符后面的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在正在制作新闻和评论系统,但我暂时停留在一部分。我希望用户能够以 @用户名的推特风格引用其他玩家。脚本看起来像这样:(不是真正的PHP,只是想象脚本; 3)
I'm making a news and comment system at the moment, but I'm stuck at one part for a while now. I want users to be able to refer to other players on the twitter style like @username. The script will look something like this: (not real PHP, just imagination scripting ;3)
$string = "I loved the article, @SantaClaus, thanks for writing!";
if($string contains @){
$word = word after @;
$check = is word in database? ...
}
而对于字符串中的所有@用户名,与while()。
And that for all the @username's in the string, perhaps done with a while(). I'm stuck, please help.
推荐答案
这是正则表达式的位置。
<?php
$string = "I loved the article, @SantaClaus! And I agree, @Jesus!";
if (preg_match_all('/(?<!\w)@(\w+)/', $string, $matches))
{
$users = $matches[1];
// $users should now contain array: ['SantaClaus', 'Jesus']
foreach ($users as $user)
{
// check $user in database
}
}
?>
-
/
regular-expressions.info 似乎是一个受欢迎的教程选择,但有很多人在网上。
regular-expressions.info seems to be a popular choice of tutorial, but there are plenty of others online.
这篇关于检查PHP中的“@”字符后面的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文