2015-02-09 97 views
2

所以我想找到一个字符串的用户名并把它们放在一个数组中,我做了正则表达式并且它返回匹配,但是当有两个匹配时它只放第一个在数组中。任何人都可以看到我的正则表达式有什么问题吗?正则表达式寻找以@ php开头的用户名

 $reactie = 'hey @sjerd and @jeska'; 
     $pattern = '/@\w*/'; 
     preg_match($pattern, $reactie, $matches); 
     print_r($matches); 

回答

3

你需要用正确的正则表达式使用preg_match_all与字边界:

$reactie = 'hey @sjerd and @jeska'; 
$pattern = '/@\w+\b/'; 
preg_match_all($pattern, $reactie, $matches); 
print_r($matches[0]);