Get email address from mailbox line

I need to extract the email address from this line of the mailbox. I was thinking about str_replace , but the display name and email address are not static, so I don't know how to do this using regular expressions.

Example: " My name <email@example.com> " should result in " email@example.com ".

Any ideas?

Thanks Matthy

+7
php regex email
Sep 03 '10 at 18:07
source share
5 answers

at face value, the following will work:

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

but I have a suspicious suspicion that you need something better than this. There are tons of β€œofficial” email regular expression patterns, and you need to be more specific about the context and rules of the match.

+5
03 Sep '10 at 18:15
source share

You can use imap_rfc822_parse_adrlist to parse this address:

 $addresses = imap_rfc822_parse_adrlist('My name <email@gmail.com>'); 
+15
Sep 03 '10 at 18:12
source share

If you know that the string is surrounded by < and > , you can simply split it according to this.

This assumes that you will always have only one pair of < and > surrounding the string, and this does not guarantee that the result will be an email template.

Otherwise, you can always read email regular expression patterns .

+1
Sep 03 '10 at 18:13
source share

or find regular expressions (preg_match).

something like: [^ <] <([^>])>;

-one
Sep 03 '10 at 18:14
source share
 $s = 'My name <email@gmail.com>'; $s = substr(($s = substr($s, (strpos($s, '<')+1))), 0, strrpos($s, '>')); 

What this basically does is extract everything between the first <and last>

-one
Sep 03 '10 at 18:51
source share



All Articles