Regex split email address

morandi3 picture morandi3 · Jul 27, 2011 · Viewed 18.4k times · Source

I need some help with php regex, I want to "split" email address "[email protected]" to "johndoe" and "@example.com"

Until now I have this: preg_match('/<?([^<]+?)@/', '[email protected]', $matches); And I get Array ( [0] => johndoe@ [1] => johndoe)

So how I need to change regex?

Answer

Michael Berkowski picture Michael Berkowski · Jul 27, 2011
$parts = explode('@', "[email protected]");

$user = $parts[0];
// Stick the @ back onto the domain since it was chopped off.
$domain = "@" . $parts[1];