我试图使以下函数仅返回每个域 1 封电子邮件。
示例:如果我提供函数:
email1@domain.com email2@domain.com email1@domain.com email1@domain.com email3@test.co.uk
我希望它返回
email1@domain.com email3@test.co.uk
这是当前功能:
function remove_duplicates($str) {
# match all email addresses using a regular expression and store them
# in an array called $results
preg_match_all("([\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7})",$str,$results);
# sort the results alphabetically
sort($results[0]);
# remove duplicate results by comparing it to the previous value
$prev="";
while(list($key,$val)=each($results[0])) {
if($val==$prev) unset($results[0][$key]);
else $prev=$val;
}
# process the array and return the remaining email addresses
$str = "";
foreach ($results[0] as $value) {
$str .= "<br />".$value;
}
return $str;
};
任何想法如何实现这一目标?