PHP:仅从可能的 <> 中删除电子邮件地址

发布于 2024-12-02 03:44:04 字数 829 浏览 0 评论 0 原文

我需要一些帮助,仅从可能包含的 <> 中获取电子邮件地址。括号。

例如,我有以下 3 个字符串,我需要每个字符串仅返回电子邮件地址:

[email protected]

"Darth Vader" <[email protected]>

"Darth Vader" <[email protected]> "Possible additional text" (Shouldn't be here but I need to make sure the regex gets rid of it anyway just in case.)

在其中的每一个字符串中,我希望 $email 等于 [电子邮件受保护]

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:

[email protected]

"Darth Vader" <[email protected]>

"Darth Vader" <[email protected]> "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 [email protected]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2024-12-09 03:44:04

只匹配有效的电子邮件地址怎么样?我们用来检查有效性的正则表达式是:

/(([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

或者这是一个完全与 TLD 无关的正则表达式:

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

其中之一应该适用于您正在寻找的内容,并且应该涵盖大多数情况。

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文