Take the 2-minute tour ×

I've a string in the following format:

John Bo <jboe@gmail.com>, abracadbra@gmail.com, <asking@gmail.com>...

How can I parse the above string in PHP and just get the email addresses? Is there an easy way to parse?


share|improve this question
Is the string delimited in a consistent manner (commas), is it always names and email addresses? –  R0MANARMY Apr 27 '11 at 2:11
Yes, the string is always delimited using "commas" –  Rajesh Beri Apr 27 '11 at 2:12
add comment

4 Answers

up vote 0 down vote accepted

One-liner no loops!

$str = 'John Bo <jboe@gmail.com>, abracadbra@gmail.com, <asking@gmail.com>';

$extracted_emails = array_map( function($v){ return trim( end( explode( '<', $v ) ), '> ' ); }, explode( ',', $str ) );


requires PHP 5.3

share|improve this answer
Is a one liner really that impressive if at the cost of readability? –  alex Apr 27 '11 at 2:32
Well, i never said it was impressive...and honestly i find my solution more readable than yours. You have $items, then you overwrite $items, then you have $emails, and $item, and $matches. If statements, continues, multidimensional arrays, and regular expressions. I think its pretty clear what my code does, check out the variable name =) –  Galen Apr 27 '11 at 2:38
And i like to mess around with the new anonymous functions –  Galen Apr 27 '11 at 2:41
Fair enough, I see your side of it :) Maybe I just understand things better reading it procedurally then from left to right trying to keep track of what has been returned where. –  alex Apr 27 '11 at 2:42
Thanks Galen - this worked fine (as well as others). I preferred your one-liner though - less and clean code :-) –  Rajesh Beri Apr 27 '11 at 5:34
add comment

You could of course just use a regex on the string, but the RFC complaint regex is a monster of a thing.

It would also fail in the unlikely (but possible event) of a@b.com <b@a.com> (unless you really would want both extracted in that case).


$items = explode(',', $str);
$items = array_map('trim', $items);

$emails = array();

foreach($items as $item) {
    preg_match_all('/<(.*?)>/', $item, $matches);

    if (empty($matches[1])) {
       $emails[] = $item;
    $emails[] = $matches[1][0];




array(3) {
  string(14) "jboe@gmail.com"
  string(20) "abracadbra@gmail.com"
  string(16) "asking@gmail.com"
share|improve this answer
Why does it need to be exploded first? Just wrap an email regex in a capturing group and collect the matches. –  BraedenP Apr 27 '11 at 2:28
@Braeden Because matching emails with regex is a difficult task (if strictly following the RFC spec). –  alex Apr 27 '11 at 2:29
It's a long and tedious regex, but it will never fail because of a simple formatting difference. There IS a ginormous RFC-valid regex. But I suppose if he's guaranteeing the formatting, then this is fine. –  BraedenP Apr 27 '11 at 2:31
@BraedenP Yep, I didn't want to include that monster here, so I went the opposite way and match the stuff between the < and >. –  alex Apr 27 '11 at 2:34
Thanks Alex - this worked fine. –  Rajesh Beri Apr 27 '11 at 5:32
add comment

The most straight-forward way would be to (also I am terrible at regex):


$emailstring = "John Bo <jboe@gmail.com>,<other@email.com>, abracadbra@gmail.com, <asking@gmail.com>";
$emails = explode(',',$emailstring);

for ($i = 0; $i < count($emails); $i++) {
    if (strpos($emails[$i], '<') !== false) {
        $emails[$i] = substr($emails[$i], strpos($emails[$i], '<')+1);
        $emails[$i] = str_replace('>','',$emails[$i]);
    $emails[$i] = trim($emails[$i]);




share|improve this answer
but then there is a combination of Name and <email address> and I just need the email address. –  Rajesh Beri Apr 27 '11 at 2:13
how are the name and email address combination formatted? –  ianace Apr 27 '11 at 2:17
@Rajesh Beri - See my edit. –  Jared Farrish Apr 27 '11 at 2:18
add comment

Use int preg_match_all (string pattern, string subject, array matches, int flags) which will search "subject" for all matches of the regex (perl format) pattern, fill the array "matches" will all matches of the rejex and return the number of matches.

See http://www.regular-expressions.info/php.html

share|improve this answer
add comment

Your Answer


