I do have a var like this:
$mail_from = "Firstname Lastname <email@domain.com>";
I would like to receive either an
array(name=>"firstname lastname", email=>"email@domain.com")
or
the values in two separate vars ($name = "...", $email = "...")
I have been playing around with preg_replace but somehow do not get it done …
Did extensive search but did not find a way to get this done.
This is the closest I got:
$str = 'My First Name <email@domain.com>';
preg_match('~(?:"([^"]*)")?\s*(.*)~',$str,$var);
print_r($var);
echo "<br>Name: ".$var[0];
echo "<br>Mail: ".$var[2];
How do I get “email@domain.com” into $var[‘x]?
Thank you.
This works for your example and should always work, when the email is within angle brackets.
Explanation:
(?:([^<]*?)\s*)?matches optionally everything that is not a<and everything except the trailing whitespace is stored in group 1.<(.*)>matches something between angle brackets and store it in group 2.