Get email address from mailbox string

前端 未结 5 1729
梦如初夏
梦如初夏 2021-01-12 10:53

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 kno

相关标签:
5条回答
  • 2021-01-12 11:27

    I'm coming from a time and from projects where Regex was to expensive for easy string extraction.

    $s = 'My name <email@gmail.com>';
    $s = substr(($s = substr($s, (strpos($s, '<')+1))), 0, strrpos($s, '>'));
    

    Results in

    email@gmail.com
    
    0 讨论(0)
  • 2021-01-12 11:31

    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.

    0 讨论(0)
  • 2021-01-12 11:36

    or Look up regular expressions (preg_match).

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

    0 讨论(0)
  • 2021-01-12 11:49

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

    This assumes that you will always have only one pair of < and > surrounding the string, and it will not ensure that the result is an email pattern.

    Otherwise you can always read up on email regex patterns.

    0 讨论(0)
  • 2021-01-12 11:51

    You can use imap_rfc822_parse_adrlist to parse that address:

    $addresses = imap_rfc822_parse_adrlist('My name <email@gmail.com>');
    
    0 讨论(0)
提交回复
热议问题