santa santa - 3 months ago 8
PHP Question

split email from string with PHP

I need to be able to split a string that contains email's

From
information. From the string I need to extract
$NAME
and
$EMAIL
or whatever is available.

The string can be in the following formats:

"Santa Clause" <santa@example.com>

Santa Clause <santa@example.com>

<santa@example.com>

Answer
$string_to_check = '"Santa Clause" <santa@npole.com>'
$matches = array();
preg_match('/?([^<"]*)"?\s*<(\S*)>/',$string_to_check,$matches);
$matches[1] //=> Santa Claus
$matches[2] //=> santa@npole.com