Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get email address from mailbox string

Tags:

regex

php

email

I need to extract the email address out of this mailbox string. I thought of str_replace but the display name and the email address is not static so I don’t know how to do this using regular expressions.

Example: "My name <[email protected]>" should result in "[email protected]".

Any ideas?

Thanks Matthy

like image 810
matthy Avatar asked Sep 03 '10 18:09

matthy


2 Answers

You can use imap_rfc822_parse_adrlist to parse that address:

$addresses = imap_rfc822_parse_adrlist('My name <[email protected]>');
like image 59
Gumbo Avatar answered Nov 03 '22 07:11

Gumbo


at face value, the following will work:

preg_match('~<([^>]+)>~',$string,$match);

but i have a sneaking suspicion you need something better than that. There are a ton of "official" email regex patterns out there, and you should be a bit more specific about the context and rules of the match.

like image 25
Crayon Violent Avatar answered Nov 03 '22 07:11

Crayon Violent