3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function splitEmail($str) { $pattern = "/([\w\s\'\"]+[\s]+)?(<)?(([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4}))?(>)?/"; 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)); }
Output for 4.3.0 - 7.1.0
array(2) { ["name"]=> string(0) "" ["email"]=> string(11) "foo@bar.com" } array(2) { ["name"]=> string(6) "Miguel" ["email"]=> string(14) "miguel@bar.com" } array(2) { ["name"]=> string(5) ""ddd"" ["email"]=> string(9) "123@d.com" } array(2) { ["name"]=> string(4) "ll'f" ["email"]=> string(10) "ddd@ddd.ll" } array(2) { ["name"]=> string(0) "" ["email"]=> string(11) "foo@bar.com" }