richardverbruggen
richardverbruggen

Reputation: 637

Help wanted with regular expression

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?

Upvotes: 0

Views: 59

Answers (1)

Wrikken
Wrikken

Reputation: 70460

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"
  }
}

Upvotes: 1

Related Questions