Sjoerd de Wit
Sjoerd de Wit

Reputation: 2413

regex find usernames starting with @ php

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

Answers (1)

anubhava
anubhava

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

Related Questions