Search code examples
phpregexpreg-replacestrip

PHP: Strip email address only from possible <>'s


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:

darth@vader.com

"Darth Vader" <darth@vader.com>

"Darth Vader" <darth@vader.com> "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 darth@vader.com


Solution

  • 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.