开发者

Parse RFC 822 compliant addresses in a TO header

I would like to parse an email address list (like the one in a TO header) with preg_match_all to get the user name (if exists) and the E-mail. Something similar to mailparse_rfc822_parse_addresses or Mail_RFC822::parseAddressList() from Pear, but in plain PHP.

Input :

"DOE, John \(ACME\)" <john.doe@somewhere.com>, "DOE, Jane" <jane.doe@somewhere.com>

Output :

array(
    array(
        'name' => 'DOE, John (ACME)',
        'email' => 'john.doe@somewhere.com'
    ), 
    array(
        'name' => 'DOE, Jane',
        'email' => 'jane.doe@somewhere.com'
    )
)

Don't need to support strange E-mail format (/[a-z0-9._%-]+@[a-z0-9.-]+.[a-z]{2,4}/i for email part is OK).

I can't use explode because the comma can appear in the name. str_getcsv doesn't work, because I can have:

DOE, John \(ACME\) <john.doe@somewhere.com> 

as input.

Update:

For the moment, I've got this :

public static function parseAddressList($addressList)
{
    $pattern = '/^(?:"?([^<"]+)"?\s)?<?([^>]+@[^>]+)>?$/';
    if (preg_match($pattern, $addressList, $matches)) {
        return array(
            array(
                'name' => stripcslashes($matches[1]),
                'email' => $matches[2]
            )
        );
    } else {
        $parts = str_getcsv($addressList);
        $result开发者_JS百科 = array();
        foreach($parts as $part) {
            if (preg_match($pattern, $part, $matches)) {
                $result[] = array(
                    'name' => stripcslashes($matches[1]),
                    'email' => $matches[2]
                );
            }
        }
        return $result;
    }
}

but it fails on:

"DOE, \"John\"" <john.doe@somewhere.com>

I need to test on back reference the \" but I don't remember how to do this.


Finally I did it:

public static function parseAddressList($addressList)
{
    $pattern = '/^(?:"?((?:[^"\\\\]|\\\\.)+)"?\s)?<?([a-z0-9._%-]+@[a-z0-9.-]+\\.[a-z]{2,4})>?$/i';
    if (($addressList[0] != '<') and preg_match($pattern, $addressList, $matches)) {
        return array(
            array(
                'name' => stripcslashes($matches[1]),
                'email' => $matches[2]
            )
        );
    } else {
        $parts = str_getcsv($addressList);
        $result = array();
        foreach($parts as $part) {
            if (preg_match($pattern, $part, $matches)) {
                $item = array();
                if ($matches[1] != '') $item['name'] = stripcslashes($matches[1]);
                $item['email'] =  $matches[2];
                $result[] = $item;
            }
        }
        return $result;
    }
}

But I'm not sure it works for all cases.


I don't know that RFC, but if the format is always as you showed then you can try something like:

preg_match_all("/\"([^\"]*)\"\\s+<([^<>]*)>/", $string, $matches);
print_r($matches);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜