在 PHP 中,如何从文本块中提取多个电子邮件地址并将它们放入数组中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3901070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:25:30  来源:igfitidea点击:

In PHP, how do I extract multiple e-mail addresses from a block of text and put them into an array?

phpregexemail

提问by HumbleHelper

I have a block of text from which I want to extract the valid e-mail addresses and put them into an array. So far I have...

我有一个文本块,我想从中提取有效的电子邮件地址并将它们放入一个数组中。到目前为止我有...

   $string = file_get_contents("example.txt"); // Load text file contents
   $matches = array(); //create array
   $pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/'; //regex for pattern of e-mail address
   preg_match($pattern, $string, $matches); //find matching pattern

However, I am getting an array with only one address. Therefore, I am guessing I need to cycle through this process somehow. How do I do that?

但是,我得到一个只有一个地址的数组。因此,我猜我需要以某种方式循环这个过程。我怎么做?

采纳答案by stevendesu

Your code is almost perfect, you just need to replace preg_match(...)with preg_match_all(...)

你的代码几乎完美,你只需要替换preg_match(...)preg_match_all(...)

http://www.php.net/manual/en/function.preg-match.php

http://www.php.net/manual/en/function.preg-match.php

http://www.php.net/manual/en/function.preg-match-all.php

http://www.php.net/manual/en/function.preg-match-all.php

回答by Clay Hinson

You're pretty close, but the regex wouldn't catch all email formats, and you don't need to specify A-Za-z, you can just use the "i" flag to mark the entire expression as case insensitive. There are email format cases that are missed (especially subdomains), but this catches the ones I tested.

您非常接近,但正则表达式不会捕获所有电子邮件格式,并且您不需要指定 A-Za-z,您只需使用“i”标志将整个表达式标记为不区分大小写。有遗漏的电子邮件格式案例(尤其是子域),但这捕获了我测试过的案例。

$string = file_get_contents("example.txt"); // Load text file contents

// don't need to preassign $matches, it's created dynamically

// this regex handles more email address formats like [email protected], and the i makes it case insensitive
$pattern = '/[a-z0-9_\-\+]+@[a-z0-9\-]+\.([a-z]{2,3})(?:\.[a-z]{2})?/i';

// preg_match_all returns an associative array
preg_match_all($pattern, $string, $matches);

// the data you want is in $matches[0], dump it with var_export() to see it
var_export($matches[0]);

output:

输出:

array (
  0 => '[email protected]',
  1 => '[email protected]',
  2 => '[email protected]',
  3 => '[email protected]',
  4 => '[email protected]',
)

回答by Eric-Karl

I know this is not the question you asked but I noticed that your regex is not accepting any address like '[email protected]' or any address with a subdomain. You could replace it with something like :

我知道这不是您问的问题,但我注意到您的正则表达式不接受任何地址,如 ' [email protected]' 或任何带有子域的地址。你可以用类似的东西替换它:

/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/

which will reject less valid e-mail (although it is not perfect).

这将拒绝不太有效的电子邮件(尽管它并不完美)。

I also suggest you read this article on e-mail validation, it is pretty good and informative.

我还建议您阅读有关电子邮件验证的这篇文章,它非常好且内容丰富。

回答by T.Todua

This detects all mail addresses:

这将检测所有邮件地址:

$sourceeee= 'Here are examplr [email protected] and [email protected] or something more';

preg_match_all('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}/i', $sourceeee, $found_mails);

then you can use $found_mails[0]array.

那么你可以使用$found_mails[0]数组。

回答by Rebule

This regex will extract all unique email address from a url or file and output each in new line. It will consider all subdomains and prefix suffix issues. Find comfortable to use it.

此正则表达式将从 url 或文件中提取所有唯一的电子邮件地址,并在新行中输出每个地址。它将考虑所有子域和前缀后缀问题。觉得用起来很舒服。

<?
$url="http://example.com/";
$text=file_get_contents($url);
$res = preg_match_all(
"/[a-z0-9]+[_a-z0-9\.-]*[a-z0-9]+@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})/i",
$text,
$matches
);
if ($res) {
foreach(array_unique($matches[0]) as $email) {
echo $email . "<br />";
}
}
else {
echo "No emails found.";
}
?>

check here for more reference : http://www.php.net/manual/en/function.preg-match-all.php

在这里查看更多参考:http: //www.php.net/manual/en/function.preg-match-all.php

回答by Luis Rodriguez

It worked better for me:

它对我来说效果更好:

<?php
$content = "Hi my name is Joe, I can be contacted at [email protected].";
preg_match("/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/i", $content, $matches);
print $matches[0];
?>

Some of the others didn't accept domains like: [email protected]

其他一些不接受域,如:[email protected]

I found it on: http://snipplr.com/view/63938/

我在:http: //snipplr.com/view/63938/

回答by fatih

This function works fine without using regex. So it is really faster and low resource hungry.

此函数无需使用正则表达式即可正常工作。所以它真的更快,资源消耗低。

<?php
function extract_email_addresses($str){
    $emails = array();
    $str = strip_tags( $str );
    $str = preg_replace('/\s+/', ' ', $str); 
    $str = preg_replace("/[\n\r]/", "", $str); 
    $remove_chars = array (',', "<", ">", ";", "'", ". ");
    $str = str_replace( $remove_chars, ' ', $str );
    $parts = explode(' ', $str);
    if(count($parts) > 0){
        foreach($parts as $part){
            $part = trim($part);
            if( $part != '' ) {
                if( filter_var($part, FILTER_VALIDATE_EMAIL) !== false){
                    $emails[] = $part;
                }                
            }
        }
    }
    if(count($emails) > 0){
        return $emails;
    }
    else{
        return null;
    }
}

$string = "Guys, please help me to extract valid [email protected] email addresses from some text content using php
example , i have below text content in mysql database ' Life is more beautiful, and i like to explore lot please email me to [email protected]. Learn new things every day. 'from the above text content i want to extract email address '[email protected]' using php regular expressions or other method.";

$matches = extract_email_addresses( $string );
print_r($matches);

?>