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,603
  • 4
  • 27
  • 37

2 Answers2

1

Use

preg_match_all.

Look here manual

Cristian Bitoi
  • 1,557
  • 1
  • 10
  • 14
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ć
  • 42,781
  • 13
  • 77
  • 107