user6612671 user6612671 - 4 months ago 12
PHP Question

Get everything in string after @ and before whitespace

I have got this string

$string="@smn and @someone are web developers"


and i want to get
@smn
and
@someone
from string.How can i do that?

Answer

Use preg_match_all with regex /(?<!\w)@\w+/

$string="@smn and @someone are web developers";
preg_match_all('/(?<!\w)@\w+/',$string,$matches);
print_r($matches[0]);

DEMO

Comments