In PHP, I have a string like this:
在PHP中,我有一个这样的字符串:
$string = "user@domain.com MIME-Version: bla bla bla";
How do i get the email address only? Is there any easy way to get the value??
我如何才能获得电子邮件地址?有什么简单的方法可以得到这个值吗?
13 个解决方案
#1
26
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.
在每个子字符串。
#2
41
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;
}
#3
15
based from constantine regex.. works with ip address domain too.
基于从君士坦丁regex . .也可以使用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 a@b.com francis a@b words francisfueconcillo@gmail.com words 2 words123 francis@192.168.0.1";
preg_match_all($pattern, $subject, $matches);
#4
5
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;
}
#5
4
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="user@domain.com 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:
上面的脚本将产生这个结果:
user@domain.com,
#6
3
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 = "user@domain.com 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应该包含你的电子邮件地址。
#7
2
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“split”函数。
#8
1
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
检测字符串中的任何电子邮件地址
#9
1
$text = 'First Last <name@example.com>'
$emails = array_filter(filter_var_array(filter_var_array(preg_split('/\s/', $text), FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL));
#10
0
If it's really space-separated:
如果是真的空格分隔:
php > $matches = array();
php > preg_match('/^[^ ]*/', $string, $matches);
php > print_r($matches[0]);
user@domain.com
#11
0
Have a look at Regular expressions in PHP.
看看PHP中的正则表达式。
With regular expressions you can identify any text pattern in a given string. They are incredibly useful. So even though you can stick with copy-pasting a code snippet from another answer for now, you should consider digging a little more into it.
使用正则表达式,您可以识别给定字符串中的任何文本模式。他们是非常有用的。因此,即使您可以继续复制从另一个答案中复制的代码片段,您也应该考虑进一步挖掘它。
It can be a bit complex at first but it's definitely worth the effort.
一开始可能有点复杂,但绝对值得付出努力。
#12
0
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;
}
#13
0
Match to a regular expression like - ([A-Za-z0-9-]+)@([A-Za-z0-9])\\.([a-z]{3})
or something similar.
匹配正则表达式如- ([a- za -z0-9-]+)@([a- za -z0-9])\ ([a-z]{3})或类似的东西。
#1
26
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.
在每个子字符串。
#2
41
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;
}
#3
15
based from constantine regex.. works with ip address domain too.
基于从君士坦丁regex . .也可以使用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 a@b.com francis a@b words francisfueconcillo@gmail.com words 2 words123 francis@192.168.0.1";
preg_match_all($pattern, $subject, $matches);
#4
5
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;
}
#5
4
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="user@domain.com 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:
上面的脚本将产生这个结果:
user@domain.com,
#6
3
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 = "user@domain.com 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应该包含你的电子邮件地址。
#7
2
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“split”函数。
#8
1
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
检测字符串中的任何电子邮件地址
#9
1
$text = 'First Last <name@example.com>'
$emails = array_filter(filter_var_array(filter_var_array(preg_split('/\s/', $text), FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL));
#10
0
If it's really space-separated:
如果是真的空格分隔:
php > $matches = array();
php > preg_match('/^[^ ]*/', $string, $matches);
php > print_r($matches[0]);
user@domain.com
#11
0
Have a look at Regular expressions in PHP.
看看PHP中的正则表达式。
With regular expressions you can identify any text pattern in a given string. They are incredibly useful. So even though you can stick with copy-pasting a code snippet from another answer for now, you should consider digging a little more into it.
使用正则表达式,您可以识别给定字符串中的任何文本模式。他们是非常有用的。因此,即使您可以继续复制从另一个答案中复制的代码片段,您也应该考虑进一步挖掘它。
It can be a bit complex at first but it's definitely worth the effort.
一开始可能有点复杂,但绝对值得付出努力。
#12
0
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;
}
#13
0
Match to a regular expression like - ([A-Za-z0-9-]+)@([A-Za-z0-9])\\.([a-z]{3})
or something similar.
匹配正则表达式如- ([a- za -z0-9-]+)@([a- za -z0-9])\ ([a-z]{3})或类似的东西。