Using regex to extract username from email address

<?php
$email  = '[email protected]';
$domain = strstr($email, '@');
echo $domain; // prints @example.com

$user = strstr($email, '@', true); // As of PHP 5.3.0
echo $user; // prints name
?>

source


@OP, if you only want to get everything before @, just use string/array methods. No need complicated regex. Explode on "@", then remove the last element which is the domain part

$str = '"peter@john@doe"@domain.com (John Doe)';
$s = explode("@",$str);
array_pop($s); #remove last element.
$s = implode("@",$s);
print $s;

output

$ php test.php
"peter@john@doe"

The regular expression that will match and capture any character until it reaches the @ character:

([^@]+)

That seems like what you need. It'll handle all kinds of freaky variations on e-mail addresses.


I'm not sure why Ben James deleted his answer, since I feel it's better than mine. I'm going to post it here (unless he undeletes his answer):

Why use regex instead of string functions?

$parts = explode("@", "[email protected]");
$username = $parts[0];

You don't need regular expressions in this situation at all. I think using explode is a much better option, personally.


As Johannes Rössel points out in the comments, e-mail address parsing is rather complicated. If you want to be 100% sure that you will be able to handle any technically-valid e-mail address, you're going to have to write a routine that will handle quoting properly, because both solutions listed in my answer will choke on addresses like "a@b"@example.com. There may be a library that handles this kind of parsing for you, but I am unaware of it.


Maybe this variant is a bit slower than explode(), but it takes only one string:

$name = preg_replace('/@.*?$/', '', $email);