Hi All,
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!
Hi All,
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!
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 http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses
You might want also to check here:
"How to Find or Validate an Email Address" on regular-expressions.info
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);
You should consider using mailparse_rfc822_parse_addresses
if possible.
Brilliant online tool - generates regex very easily, by only clicking :D Supports not only php, but many other languages and scripts :) Easy to use, saved my back many times ^^ Enjoy!!!