查看值是否存在然后获取值

时间:2011-11-15 21:09:35

标签: php

基本上,当一个表单在评论和评论中提交时,它包含@username@个人的用户名,我想做点什么,例如:向用户名发送通知。

所以,如果它之后包含@和一个单词(带字母或数字),我想得到它并用它做点什么。

e.g。

if (contains an @ symbol, get the value of it)  {
// do something
}

3 个答案:

答案 0 :(得分:1)

$str = 'hello @user';
preg_match('/@([a-z0-9]+)/i', $str, $matches);

// $matches[0] = '@user';
// $matches[1] = 'user';

答案 1 :(得分:0)

使用此代码,您将能够检查字符串是否包含“@”。

if(strpos('My @string', '@') !== false)
{
     //Actions if it found a @ in the string
}
else
{
     //Actions if it didn't found a @ in the string
}

答案 2 :(得分:0)

如果您使用\w代替[a-z0-9],则会抓住有效_的{​​{1}}个字符。     

user_names
相关问题