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
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
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.
or Look up regular expressions (preg_match).
something like: [^<]<([^>])>;
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.
You can use imap_rfc822_parse_adrlist to parse that address:
$addresses = imap_rfc822_parse_adrlist('My name <email@gmail.com>');