Php 创建包含电子邮件的数组
我正在尝试创建一个函数来提取电子邮件发送,但我没有找到解决方案 多谢各位 电子邮件类型:Php 创建包含电子邮件的数组,php,Php,我正在尝试创建一个函数来提取电子邮件发送,但我没有找到解决方案 多谢各位 电子邮件类型:联系人,联系人1 功能 public static function extractEmail(string $email) { $pattern = '/(?<=<)(.*?)+(?=\>)/'; $result = preg_match_all($pattern , $email , $matches); $result = explod
联系人,联系人1
功能
public static function extractEmail(string $email)
{
$pattern = '/(?<=<)(.*?)+(?=\>)/';
$result = preg_match_all($pattern , $email , $matches);
$result = explode(',', $result);
if (is_array($result)) {
foreach ($result as $token) {
$email = filter_var(filter_var($token, FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL);
if ($email !== false) {
$emails[] = $email;
}
}
return $emails[0];
} else {
return false;
}
}
公共静态函数提取电子邮件(字符串$email)
{
$pattern='/(?您可以使用和清理电子邮件地址并删除无效地址:
/**
* @return string[]
*/
function extractEmails(string $contactString): array
{
if (!preg_match_all('/<(?<emails>[^>]+)>/', $contactString, $matches)) {
return [];
}
return array_filter(array_map(static function (string $email): ?string {
$sanitizedEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
return filter_var($sanitizedEmail, FILTER_VALIDATE_EMAIL) ? $sanitizedEmail : null;
}, $matches['emails']));
}
/**
*@returnstring[]
*/
函数提取电子邮件(字符串$contactString):数组
{
如果(!preg_match_all('/]+)>/',$contactString,$matches)){
返回[];
}
返回数组过滤器(数组映射)(静态函数(字符串$email):?字符串{
$sanitizedmail=filter\u var($email,filter\u SANITIZE\u email);
返回筛选器变量($sanitizedEmail,筛选器验证\电子邮件)$sanitizedEmail:null;
},$matches['emails']);
}
用法:
print_r(extractEmails('contact<contact@contact.com>, contact1<contact1@contact.com>'));
// ['contact@contact.com', 'contact1@contact.com']
print_r(提取电子邮件('contact,contact1');
// ['contact@contact.com', 'contact1@contact.com']
基本上:
preg\u match\u all
收集$matches['email']中的所有(假定)电子邮件地址。
array\u map
清理并验证所述电子邮件地址(null
如果其中一个地址无效,则返回该地址)
array\u filter
最终删除所有null
值
您是否打开了错误报告功能?您应该会收到多个通知。