How do I grab the email inside a string using Regex?
My string is as follows
"First Last <first.last@email.com>"
I want to grab "first.last@gmail.com" and store it somewhere.
Thanks in advance!
How do I grab the email inside a string using Regex?
My string is as follows
"First Last <first.last@email.com>"
I want to grab "first.last@gmail.com" and store it somewhere.
Thanks in advance!
Without Regex (and likely much faster):
$string = "First Last <first.last@email.com>";
echo substr($string, strpos($string, '<') +1, -1);
or
echo trim(strstr("First Last <first.last@email.com>", '<'), '<>');
will both give
first.last@email.com
If you need to validate the final outcome, use
filter_var($eMailString, FILTER_VALIDATE_EMAIL);
In your example, I'll do something like:
preg_match('/<([^>]+)>/', "First Last <first.last@email.com>", $matches);
$email = $matches[1];
Check out the official PHP documentation on preg_match.
^[^<]*<([^>]*)>$
For the rest, see Using a regular expression to validate an email address