0

I have this code which does exactly what im after. However it only does it for one username, was wondering how I could do it so it gets more than one username from it?

$string = 'Tweet @one two @three four';
preg_match("/@(\\w+)/", $string, $matches);
$hash1 = $matches[1];
echo $hash1;

$hash1 returns "one".

Elon Than
  • 9,292
  • 4
  • 25
  • 37

2 Answers2

1

Use

preg_match_all.

Look here manual

Cristian Bitoi
  • 1,512
  • 1
  • 10
  • 13
1

Use preg_match_all function:

$string = 'Tweet @one two @three four';
preg_match_all("/@(\w+)\b/", $string, $matches, PREG_SET_ORDER);
print_r($matches);
Glavić
  • 41,315
  • 13
  • 72
  • 105