Reputation: 484
I need a little assistance getting email addresses only from within their POSSIBLY INCLUDED <> brackets.
For example I have the following 3 strings and I need each one to return only the email address:
[email protected]
"Darth Vader" <[email protected]>
"Darth Vader" <[email protected]> "Possible additional text" (Shouldn't be here but I need to make sure the regex gets rid of it anyway just in case.)
On every single one of those I would want $email to equal [email protected]
Upvotes: 0
Views: 300
Reputation: 13614
How about just matching for valid e-mail addresses? The regex we use to check validity is:
/(([a-z0-9!#$%&*+-=?^_`{|}~][a-z0-9!#$%&*+-=?^_`{|}~.]*[a-z0-9!#$%&*+-=?^_`{|}~])|[a-z0-9!#$%&*+-?^_`{|}~]|("[^"]+"))\@([-a-z0-9]+\.)+(com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum|co|co\.uk)/img
Or here's one that's completely TLD-agnostic:
/(([a-z0-9&*\+\-\=\?^_`{|\}~][a-z0-9!#$%&*+-=?^_`{|}~.]*[a-z0-9!#$%&*+-=?^_`{|}~])|[a-z0-9!#$%&*+-?^_`{|}~]|("[^"]+"))\@([-a-z0-9]+\.)+([a-z]{2,})/img
One of those should work for what you're looking for and should cover most cases.
Upvotes: 4