<?php
function splitEmail($str)
{
$pattern = "/([\w\s\'\"]+[\s]+)?(<)?(([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4}))?(>)?/g";
preg_match($pattern, $str, $match);
return array(
'name' => isset($match[1]) ? trim($match[1]) : null,
'email' => isset($match[3]) ? trim($match[3]) : null,
);
}
$e = array(
'<foo@bar.com>',
'Miguel <miguel@bar.com>',
'"ddd" <123@d.com>',
'll\'f <ddd@ddd.ll>',
'foo@bar.com',
);
foreach($e as $v) {
var_dump($splitEmail($v));
}
preferences:
41.89 ms | 402 KiB | 5 Q