How do I grab the email inside a string using Regex?
My string is as follows
"First Last <[email protected]>"
I want to grab "[email protected]" 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 <[email protected]>"
I want to grab "[email protected]" and store it somewhere.
Thanks in advance!
Without Regex (and likely much faster):
$string = "First Last <[email protected]>";
echo substr($string, strpos($string, '<') +1, -1);
or
echo trim(strstr("First Last <[email protected]>", '<'), '<>');
will both give
[email protected]
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 <[email protected]>", $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