tags:

views:

38

answers:

1

I would like to parse a mailer to-string which could consist of the following examples separated with a comma (,):

First name <[email protected]>
"first name" <[email protected]>
<[email protected]>
[email protected]

I would like to make an array with an entry for each element with two sub-entries: [name] and [email].

I've been struggling with the regexp for (what looks like) ages. Could someone help me?

+1  A: 

If you have the imap extension enabled, it may be as simple as:

var_dump(imap_rfc822_parse_adrlist('First name <[email protected]>,
    "first name" <[email protected]>,
    <[email protected]>,
    [email protected]','_invalid_'));

Output:

array(4) {
  [0]=>
  object(stdClass)#1 (3) {
    ["mailbox"]=>
    string(5) "email"
    ["host"]=>
    string(11) "example.com"
    ["personal"]=>
    string(10) "First name"
  }
  [1]=>
  object(stdClass)#2 (3) {
    ["mailbox"]=>
    string(5) "email"
    ["host"]=>
    string(11) "example.com"
    ["personal"]=>
    string(10) "first name"
  }
  [2]=>
  object(stdClass)#3 (2) {
    ["mailbox"]=>
    string(5) "email"
    ["host"]=>
    string(11) "example.com"
  }
  [3]=>
  object(stdClass)#4 (2) {
    ["mailbox"]=>
    string(5) "email"
    ["host"]=>
    string(11) "example.com"
  }
}
Wrikken
Ow that's a lot lot lot easier!!!
richardverbruggen
The regex for parsing email addresses that comply with with the rfc822 spec is a bit complex. http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html +1
Buggabill
@Buggabill The regexp wouldn't actually need to validate the e-mail address, just find it within the main string, which isn't particularly difficult
Michael Mrozek