开发者

PHP: Strip email address only from possible <>'s

I need a little assistance getting email addresses only from within their POSSIBLY INCLUDED <> brackets.

For example I have the following 3 strings and I need each one to return only the email address:

darth@vader.com

"Darth Vader" <darth@vader.com>

"Darth Vader" <darth@vader.com> "Possible additional text" (Shouldn't be here but I need to make sure the regex gets rid开发者_如何学编程 of it anyway just in case.)

On every single one of those I would want $email to equal darth@vader.com


How about just matching for valid e-mail addresses? The regex we use to check validity is:

/(([a-z0-9!#$%&*+-=?^_`{|}~][a-z0-9!#$%&*+-=?^_`{|}~.]*[a-z0-9!#$%&*+-=?^_`{|}~])|[a-z0-9!#$%&*+-?^_`{|}~]|("[^"]+"))\@([-a-z0-9]+\.)+(com|net|edu|org|gov|mil|int|biz|pro|info|arpa|aero|coop|name|museum|co|co\.uk)/img

(reFiddle link)

Or here's one that's completely TLD-agnostic:

/(([a-z0-9&*\+\-\=\?^_`{|\}~][a-z0-9!#$%&*+-=?^_`{|}~.]*[a-z0-9!#$%&*+-=?^_`{|}~])|[a-z0-9!#$%&*+-?^_`{|}~]|("[^"]+"))\@([-a-z0-9]+\.)+([a-z]{2,})/img

(reFiddle link)

One of those should work for what you're looking for and should cover most cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜