我想从字符串中提取电子邮件地址,例如:
<?php // code
$string = 'Ruchika <ruchika@example.com>';
?>
从上面的字符串中我只想获得电子邮件地址ruchika@example.com
。
请注意,建议如何实现这一目标。
答案 0 :(得分:21)
解析电子邮件地址是一项疯狂的工作,会导致非常复杂的正则表达式。例如,请考虑使用此官方正则表达式来捕获电子邮件地址:http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
太棒了吧?
相反,有一个标准的PHP函数来执行此操作,称为mailparse_rfc822_parse_addresses()
并记录here。
它接受一个字符串作为参数,并返回一个关联数组数组,其中包含键display,address和is_group。
所以,
$to = 'Wez Furlong <wez@example.com>, doe@example.com';
var_dump(mailparse_rfc822_parse_addresses($to));
会产生:
array(2) {
[0]=>
array(3) {
["display"]=>
string(11) "Wez Furlong"
["address"]=>
string(15) "wez@example.com"
["is_group"]=>
bool(false)
}
[1]=>
array(3) {
["display"]=>
string(15) "doe@example.com"
["address"]=>
string(15) "doe@example.com"
["is_group"]=>
bool(false)
}
}
答案 1 :(得分:19)
试试这个
<?php
$string = 'Ruchika < ruchika@example.com >';
$pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
preg_match_all($pattern, $string, $matches);
var_dump($matches[0]);
?>
请参阅demo here
第二种方法
<?php
$text = 'Ruchika < ruchika@example.com >';
preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $text, $matches);
print_r($matches[0]);
?>
请参阅demo here
答案 2 :(得分:2)
试试这段代码。
<?php
function extract_emails_from($string){
preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
return $matches[0];
}
$text = "blah blah blah blah blah blah email2@address.com";
$emails = extract_emails_from($text);
print(implode("\n", $emails));
?>
这会奏效。
感谢。
答案 3 :(得分:2)
这是基于Niranjan的回复,假设您输入的电子邮件包含在&lt;和&gt;字符)。而不是使用正则表达式来获取电子邮件地址,这里我得到&lt;之间的文本部分。和&gt;字符。否则,我使用该字符串来获取整个电子邮件。当然,我没有对电子邮件地址进行任何验证,这取决于您的情况。
<?php
$string = 'Ruchika <ruchika@example.com>';
$pattern = '/<(.*?)>/i';
preg_match_all($pattern, $string, $matches);
var_dump($matches);
$email = $matches[1][0] ?? $string;
echo $email;
?>
当然,如果我的假设不正确,那么这种方法就会失败。但根据您的输入,我相信您想要提取封闭在&lt;和&gt;字符。
答案 4 :(得分:1)
这很好用,而且很简单:
$email = strpos($from, '<') ? substr($from, strpos($from, '<') + 1, -1) : $from
答案 5 :(得分:0)
基于Priya Rajaram的代码,我对功能进行了更多优化,以便每个电子邮件地址仅出现一次。
例如,如果解析了HTML文档,则通常会两次获得所有内容,因为mailto链接中也使用了邮件地址。
function extract_emails_from($string){
preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches);
return array_values(array_unique($matches[0]));
}
答案 6 :(得分:-1)
您也可以尝试:
email=re.findall(r'\S+@\S+','ruchika@example.com')
print email
其中\S
表示任何非空白字符
答案 7 :(得分:-1)
找到了一些有用的codes,如下:
<?php
// input: My Test Email <some.test.email@somewhere.net>
function get_displayname_from_rfc_email($rfc_email_string) {
// match all words and whitespace, will be terminated by '<'
$name = preg_match('/[\w\s]+/', $rfc_email_string, $matches);
$matches[0] = trim($matches[0]);
return $matches[0];
}
// Output: My Test Email
function get_email_from_rfc_email($rfc_email_string) {
// extract parts between the two parentheses
$mailAddress = preg_match('/(?:<)(.+)(?:>)$/', $rfc_email_string, $matches);
return $matches[1];
}
// Output: some.test.email@somewhere.net
?>
希望这对某人有帮助。
答案 8 :(得分:-1)
使用(我的)函数getEmailArrayFromString
从给定的字符串中轻松提取电子邮件地址。
<?php
function getEmailArrayFromString($sString = '')
{
$sPattern = '/[\._\p{L}\p{M}\p{N}-]+@[\._\p{L}\p{M}\p{N}-]+/u';
preg_match_all($sPattern, $sString, $aMatch);
$aMatch = array_keys(array_flip(current($aMatch)));
return $aMatch;
}
// Example
$sString = 'foo@example.com XXX bar@example.com XXX <baz@example.com>';
$aEmail = getEmailArrayFromString($sString);
/**
* array(3) {
[0]=>
string(15) "foo@example.com"
[1]=>
string(15) "bar@example.com"
[2]=>
string(15) "baz@example.com"
}
*/
var_dump($aEmail);
答案 9 :(得分:-1)
这甚至可以在子域上使用。它从文本中提取所有电子邮件。
$marches[0]
具有所有电子邮件。
$pattern = "/[a-zA-Z0-9-_]{1,}@[a-zA-Z0-9-_]{1,}(.[a-zA-Z]{1,}){1,}/";
preg_match_all ($pattern , $string, $matches);
print_r($matches);
$marches[0]
具有所有电子邮件。
Array
(
[0] => Array
(
[0] => clotdesormakilgehr@prednisonecy.com
[1] => **********@******.co.za.com
[2] => clotdesormakilgehr@prednisonecy.com
[3] => clotdesormakilgehr@prednisonecy.mikedomain.com
[4] => clotdesormakilgehr@prednisonecy.com
)
[1] => Array
(
[0] => .com
[1] => .com
[2] => .com
[3] => .com
[4] => .com
)
)
答案 10 :(得分:-1)
一种相对简单的方法是使用PHP内置方法将文本拆分为单词并验证电子邮件:
function fetchEmails($text) {
$words = str_word_count($text, 1, '.@-_');
return array_filter($words, function($word) {return filter_var($word, FILTER_VALIDATE_EMAIL);});
}
将在text变量内返回电子邮件地址。