我想从字符串中提取电子邮件地址,例如:
<?php // code
$string = 'Ruchika <ruchika@example.com>';
?>
从上面的字符串中,我只想得到电子邮件地址ruchika@example.com
。
请推荐如何实现这一点。
试试这个
<?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]);
?>
点击此处查看演示
第二种方法
<?php
$text = 'Ruchika < ruchika@example.com >';
preg_match_all("/['._a-zA-Z0-9-]+@['._a-zA-Z0-9-]+/i", $text, $matches);
print_r($matches[0]);
?>
请参阅此处的演示
解析电子邮件地址是一项疯狂的工作,会导致非常复杂的正则表达式。例如,考虑以下官方正则表达式来捕获电子邮件地址:http://www.ex-parrot.com/pdw/Mail-RFC822-Address.html
太神奇了,对吧?
相反,有一个标准的php函数来实现这一点,称为mailparse_rfc822_parse_addresses()
,并在这里进行了说明。
它以一个字符串作为参数,并返回一个关联数组数组,其中包含关键字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)
}
}
试试这个代码。
<?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));
?>
这会奏效的。
谢谢。
这是基于Niranjan的回复,假设您的输入电子邮件包含在<和>个字符)。我没有使用正则表达式来获取电子邮件地址,而是在这里获得<和>个字符。否则,我会使用字符串来获取整个电子邮件。当然,我没有对电子邮件地址进行任何验证,这将取决于您的情况。
<?php
$string = 'Ruchika <ruchika@example.com>';
$pattern = '/<(.*?)>/i';
preg_match_all($pattern, $string, $matches);
var_dump($matches);
$email = $matches[1][0] ?? $string;
echo $email;
?>
这是一个分叉的演示。
当然,如果我的假设不正确,那么这种方法就会失败。但根据你的输入,我相信你想提取<和>个字符。
此函数从字符串中提取所有电子邮件并以数组形式返回。
function extract_emails_from($string){
preg_match_all( '/(['w+'.]*'w+@['w+'.]*'w+['w+'-'w+]*'.'w+)/is', $string, $matches );
return $matches[0];
};
这很好用,而且很小:
$email = strpos($from, '<') ? substr($from, strpos($from, '<') + 1, -1) : $from
使用(my)函数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);
基于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]));
}
这甚至可以在子域上使用。它从文本中提取所有电子邮件。
$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
)
)
一种相对直接的方法是使用PHP内置的方法将文本拆分为单词并验证电子邮件:
function fetchEmails($text) {
$words = str_word_count($text, 1, '.@-_1234567890');
return array_filter($words, function($word) {return filter_var($word, FILTER_VALIDATE_EMAIL);});
}
将返回文本变量中的电子邮件地址。