2

I have an email address that could either be $email = "x@example.com"; or $email="Johnny <x@example.com>" I want to get $handle = "x"; for either version of the $email. How can this be done in PHP (assuming regex). I'm not so good at regex.

Thanks in advance

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
willium
  • 2,048
  • 5
  • 25
  • 34
  • 1
    Note that (assuming the email address you're working with is coming from the "From:" line of an RFC2822 mail message, or equivalent), it's perfectly possible to have an address with @'s or angle brackets quoted inside the display name. For example: `"Johnny @ home" ` – David Gelhar Jan 07 '11 at 01:42
  • 2
    David's right. None of the answers below will cover the entire range of valid email addresses. It's a reasonably hard problem, and to do it right will probably require a parser, not a regex. But marcog's solution will catch some 97% of cases (give or take), and that may be good enough for your needs. – dkarp Jan 07 '11 at 03:39
  • this is really not an issue but, thank you for pointing that out. Username@myservice.com will be the email, and if it's something weird, then we'll just toss it out. – willium Jan 07 '11 at 07:29

4 Answers4

3

Use the regex <?([^<]+?)@ then get the result from $matches[1].

Here's what it does:

  • <? matches an optional <.
  • [^<]+? does a non-greedy match of one or more characters that are not ^ or <.
  • @ matches the @ in the email address.

A non-greedy match makes the resulting match the shortest necessary for the regex to match. This prevents running past the @.

Rubular: http://www.rubular.com/r/bntNa8YVZt

moinudin
  • 134,091
  • 45
  • 190
  • 216
3

Here is a complete PHP solution based on marcog's answer

function extract_email($email_string) {
    preg_match("/<?([^<]+?)@([^>]+?)>?$/", $email_string, $matches);
    return $matches[1] . "@" . $matches[2];
}

echo extract_email("ice.cream.bob@gmail.com"); // outputs ice.cream.bob@gmail.com
echo extract_email("Ice Cream Bob <ice.cream.bob@gmail.com>"); // outputs ice.cream.bob@gmail.com
wom
  • 180
  • 1
  • 8
0

Just search the string using this basic email-finding regex: \b[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}\b It will match any email in any text, and in your first string it will match the whole string, and in the second, only the part of the string that is e-mail.

To quickly learn regexp this is the best place: http://www.regular-expressions.info

Cray
  • 2,396
  • 19
  • 29
  • 1
    Correctly matching email addresses with a regex is a LOT harder than that. (For example, not all top-level domain names are 2-4 characters long, and this regex disallows many valid characters in the local-part of the address.) Many other questions like this, such as http://stackoverflow.com/questions/611775/regular-expression-for-valid-email-address – David Gelhar Jan 07 '11 at 01:30
0
$email = 'x@gmail.com';
preg_match('/([a-zA-Z0-9\-\._\+]+@[a-z0-9A-Z\-\._]+\.[a-zA-Z]+)/', $email, $regex);
$handle = array_shift(explode('@', $regex[1]));

Try that (Not tested)

Robert Ross
  • 1,895
  • 2
  • 23
  • 32