如何使用正則表達式獲取字符串內部的電子郵件?簡單正則表達式幫助
我的字符串如下
"First Last <[email protected]>"
我要搶「[email protected]」,並存儲在某個地方。
在此先感謝!
如何使用正則表達式獲取字符串內部的電子郵件?簡單正則表達式幫助
我的字符串如下
"First Last <[email protected]>"
我要搶「[email protected]」,並存儲在某個地方。
在此先感謝!
^[^<]*<([^>]*)>$
對於剩下的,看Using a regular expression to validate an email address
在你的榜樣,我會做這樣的事情:
preg_match('/<([^>]+)>/', "First Last <[email protected]>", $matches);
$email = $matches[1];
沒有正則表達式(可能更快):
$string = "First Last <[email protected]>";
echo substr($string, strpos($string, '<') +1, -1);
或
echo trim(strstr("First Last <[email protected]>", '<'), '<>');
都將給予
[email protected]
如果您需要驗證的最終結果,使用
filter_var($eMailString, FILTER_VALIDATE_EMAIL);
退房:http://www.phpfreaks.com/forums/index.php?topic=235338.0 – Burton 2010-02-22 17:18:15
您可能還需要在這裏查看: > [「如何查找或驗證電子郵件地址」](http://www.regular-expressions.info/email.html)on regular-expressions.info – npinti 2010-02-22 17:21:26
您應該考慮使用['mailparse_rfc822_parse_addresses'](http:/如果可能的話,/php.net/mailparse_rfc822_parse_addresses)。 – Gumbo 2010-02-22 18:07:19