Laravel 5 - 从数组中提取电子邮件?

时间:2015-08-19 16:41:48

标签: arrays laravel-5

我环顾四周但是没有找到解决方案或方法在Laravel中开箱即用,所以我想知道是否有办法做到这一点:

我有一个$emails数组,其中2封电子邮件有效,1封无效:

$emails = ["apple@fruits.com","banana@fruits.com","apple.com","banana"];

// How can I get the following 2 arrays using Laravel?
$validEmails = ["apple@fruits.com","banana@fruits.com"];
$invalidEmails = ["apple.com","banana"];

如何从$emails数组中使用Laravel获取最后2个数组?

1 个答案:

答案 0 :(得分:1)

可能你不需要Laravel。

$validEmails = [];
$invalidEmails = [];
foreach ($emails as $email){
   if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
         $validEmails[] = $email;
   }
   else
   {
         $invalidEmails[] = $email;
   }
}

[注意:它将在PHP 5.2之后运行]