我尝试验证多个电子邮件地址,这些电子邮件地址用逗号分隔,或只是一个电子邮件地址,具体取决于用户条目。
我创建了这个函数:
function isEmailMultiple($address)
{
$validate = array();
if(strlen($address) < 1)
{
return false;
}
$emailAddresses = explode(",", $address);
foreach($emailAddresses as $emailAdd)
{
$emailAdd = trim($emailAdd);
if (!preg_match('/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@([a-z0-9])*(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i', $emailAdd))
{
// if validation fails, add error to array
$validate[$emailAdd] = 1;
}
}
if(count($validate) == 0)
{
// No errors added to the array
return true;
}
else
{
return false;
}
}
这是内爆功能:
$headers .= 'BCC: '. implode(",", $emailfromform) . "\r\n";
我收到此错误:
Warning: implode() [function.implode]: Invalid arguments passed in. . .
答案 0 :(得分:4)
由于$emailfromform
只是一个值(字符串),因此您无法使用implode()
,并且只接受数组(将它们转换为字符串)。
现在,如果您要确保用逗号分隔电子邮件地址,可以尝试这样的事情:
// split email address by space, comma, colon, or semi-colon
$emailfromform = preg_split("/[\s,;:]+/", $emailfromform);
// Put them back together separated by a comma
$emailfromform = implode(",", $emailfromform);
// add to the headers
$headers .= 'BCC: '. $emailfromform . "\r\n";
然后验证电子邮件地址只需使用您的功能。这是我写的内容的一些改进版本:
function isEmailMultiple(array $emails) {
if(count($emails) === 0) {
return false;
}
$emailAddresses = explode(",", $emails);
foreach($emailAddresses as $emailAdd) {
$emailAdd = trim($emailAdd);
if (!filter_var($emailAdd, FILTER_VALIDATE_EMAIL)) {
return false;
}
}
return true;
}
答案 1 :(得分:0)
function isEmailMultiple($address)
$address = array();
您覆盖参数。并使用新变量作为内爆函数的参数
删除$address = array();
行,然后重试