Reputation: 2413
so i want to find usernames in a string and put them in an array, i've made the regex and it returns the match, but when there are 2 matches it only puts the first one in the array. Can anyone see what is wrong with my regex?
$reactie = 'hey @sjerd and @jeska';
$pattern = '/@\w*/';
preg_match($pattern, $reactie, $matches);
print_r($matches);
Upvotes: 2
Views: 50
Reputation: 785286
You need to use preg_match_all
with correct regex with word boundary:
$reactie = 'hey @sjerd and @jeska';
$pattern = '/@\w+\b/';
preg_match_all($pattern, $reactie, $matches);
print_r($matches[0]);
Upvotes: 3