php 如何从长字符串中获取电子邮件地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1028553/
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
How to get email address from a long string
提问by Rob Locke
In PHP, I have a string like this:
在 PHP 中,我有一个这样的字符串:
$string = "[email protected] MIME-Version: bla bla bla";
How do i get the email address only? Is there any easy way to get the value??
我如何只获取电子邮件地址?有什么简单的方法可以获得价值吗?
采纳答案by mandaleeka
If you're not sure which part of the space-separated string is the e-mail address, you can split the string by spaces and use
如果您不确定用空格分隔的字符串的哪一部分是电子邮件地址,您可以用空格分割字符串并使用
filter_var($email, FILTER_VALIDATE_EMAIL)
on each substring.
在每个子串上。
回答by Rob Locke
Building on mandaleeka's answer, break the string up using a space delimeter then use filter_var to sanitize then validate to see if what remains is a legitimate email address:
以 mandaleeka 的回答为基础,使用空格分隔符将字符串分解,然后使用 filter_var 进行清理,然后验证以查看剩下的是否是合法的电子邮件地址:
function extract_email_address ($string) {
foreach(preg_split('/\s/', $string) as $token) {
$email = filter_var(filter_var($token, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
if ($email !== false) {
$emails[] = $email;
}
}
return $emails;
}
回答by user1046762
based from constantine regex.. works with ip address domain too.
基于康斯坦丁正则表达式.. 也适用于 IP 地址域。
$pattern="/(?:[A-Za-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[A-Za-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[A-Za-z0-9-]*[A-Za-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/";
//$pattern="/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/";
$subject="Hello [email protected] francis a@b words [email protected] words 2 words123 [email protected]";
preg_match_all($pattern, $subject, $matches);
回答by orafaelreis
Updating @Rob Locken's answers:
更新@Rob Locken 的回答:
function extract_email_address ($string) {
$emails = array();
$string = str_replace("\r\n",' ',$string);
$string = str_replace("\n",' ',$string);
foreach(preg_split('/ /', $string) as $token) {
$email = filter_var($token, FILTER_VALIDATE_EMAIL);
if ($email !== false) {
$emails[] = $email;
}
}
return $emails;
}
回答by Mitul Nakum
This small PHP script will help us to extract the email address from a long paragraph or text. Just copy paste this script and save it as a PHP file (extract.php):
这个小的 PHP 脚本将帮助我们从长段落或文本中提取电子邮件地址。只需复制粘贴此脚本并将其另存为 PHP 文件 (extract.php):
$string="[email protected] MIME-Version: bla bla bla";
$pattern="/(?:[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/";
preg_match_all($pattern, $string, $matches);
foreach($matches[0] as $email){
echo $email.", ";
}
?>
The above script will produce this result:
上面的脚本会产生这样的结果:
[email protected],
回答by Gabriel Hurley
Email addresses are really tricky to filter using regular expressions because there are so many possible allowable characters. It can be done, but you may have to tweak it some to get exactly what you need.
使用正则表达式过滤电子邮件地址真的很棘手,因为有很多可能的允许字符。它可以完成,但您可能需要对其进行一些调整才能准确获得所需的内容。
You could start with something like this:
你可以从这样的事情开始:
$string = "[email protected] MIME-Version: bla bla bla";
$matches = array();
$pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/'
preg_match($pattern,$string,$matches);
And then $matches should contain your email address.
然后 $matches 应该包含您的电子邮件地址。
回答by ravuya
If the email address is always at the front of the string, the easiest way to get it is simply to split the string on all instances of the space character, and then just take the first value from the resulting array.
如果电子邮件地址始终位于字符串的前面,那么获取它的最简单方法就是在空格字符的所有实例上拆分字符串,然后从结果数组中取出第一个值。
Of course, make sure to check it is something resembling an email address before you use it.
当然,在使用之前,请务必检查它是否类似于电子邮件地址。
See the PHP 'split' function for details.
有关详细信息,请参阅 PHP 的“拆分”函数。
回答by Constantin
this worked for me
这对我有用
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
detect any email address in a string
检测字符串中的任何电子邮件地址
回答by Sureshkumar
$text = 'First Last <[email protected]>'
$emails = array_filter(filter_var_array(filter_var_array(preg_split('/\s/', $text), FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL));
回答by Keith Ritter
I also modified @Rob Locke's answer. I found that it didnt work for me because I had to first split by commas then by spaces.
我还修改了@Rob Locke 的回答。我发现它对我不起作用,因为我必须先用逗号分隔,然后用空格分隔。
function extract_email_addresses($sString)
{
$aRet = array();
$aCsvs = explode(',', $sString);
foreach($aCsvs as $sCsv)
{
$aWords = explode(' ', $sCsv);
foreach($aWords as $sWord)
{
$sEmail = filter_var(filter_var($sWord, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
if($sEmail !== false)
$aRet[] = $sEmail;
}
}
return $aRet;
}

