php正则表达式:如何获取段落中的所有电子邮件地址

发布于 2024-09-16 13:17:12 字数 44 浏览 2 评论 0原文

我想获取一个段落中的所有电子邮件地址。你能帮我吗

非常感谢

I want to fetch all the email address in a paragraph. could you please help me

Thanks a lot

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

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

发布评论

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

评论(5

埖埖迣鎅 2024-09-23 13:17:12

在此处查找电子邮件正则表达式:

http://www.regular-expressions.info/email.html< /a>

它甚至有支持电子邮件正则表达式的“官方标准:RFC 2822”。

Look here for Email regexes:

http://www.regular-expressions.info/email.html

It even has the "The Official Standard: RFC 2822" supporting email regexes.

茶色山野 2024-09-23 13:17:12

您可以使用 preg_match_all 函数。语法如下:

//Filter our the email addresses and store it in $emails
preg_match($regexp,$text,$emails);

//Print out the array of emails
print_r($emails);

其中:

  • $regexp 是电子邮件的正则表达式
  • $text 是文本段落
  • $emails 是电子邮件地址的输出数组

正则表达式可以根据您希望它的松紧程度而变化。尝试制作您自己的,以便它遵循您的标准,但如果您遇到问题,您可以使用 Google 电子邮件正则表达式。

PS 我希望您不打算用它来发送垃圾邮件。

You can use the preg_match_all function. The syntax would be like this:

//Filter our the email addresses and store it in $emails
preg_match($regexp,$text,$emails);

//Print out the array of emails
print_r($emails);

where:

  • $regexp is a regular expression for an email
  • $text is the paragraph of text
  • $emails is the output array of email addresses

A regular expression can vary depending on how loose or tight you want it to be. Try crafting your own, so that it follows your standards, but if you're having trouble, you can just Google Email Regular Expressions.

P.S. I hope you're not planning to use this to spam somehow.

梦过后 2024-09-23 13:17:12

正如其他人所建议的,请在您的问题上投入更多精力。

以下是您所追求的一个非常基本的实现。它确实会忽略某些电子邮件类型,但是嘿,这与您投入的精力相同。

  $matches = preg_match_all(
    "/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
    $text,
    $emails
  );

$text 是您的段落。
$emails 是匹配值的数组。

As suggested by others, please put more for of an effort into your questions.

The following is a very basic implementation of what you're after. It does ignore some email types, but hey, its the same amount of effort you put in.

  $matches = preg_match_all(
    "/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
    $text,
    $emails
  );

$text is your paragraph.
$emails is the array of matched values.

GRAY°灰色天空 2024-09-23 13:17:12
<?php
$paragraph="this is test [email protected] [email protected] testing";
$pattern="/([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)@([ ]+|)([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)/i"; preg_match_all($pattern, $paragraph, $matches);

print_r($matches[0]);
?>
<?php
$paragraph="this is test [email protected] [email protected] testing";
$pattern="/([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)@([ ]+|)([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)/i"; preg_match_all($pattern, $paragraph, $matches);

print_r($matches[0]);
?>
不交电费瞎发啥光 2024-09-23 13:17:12

试试这个:
正则表达式:/[-.\w]+@[-.\w]+/i

Try this:
regEx: /[-.\w]+@[-.\w]+/i

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