Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove invalid email format in PHP

Array (
    [0] => [email protected]
    [1] => [email protected]
    [2] => invalidEmail.com
)

Notice that the third array value is invalid email format. How do I remove it by using/creating a function? I need the valid email to implode("," the valid email) before sending an email using the mail() function.

like image 589
softboxkid Avatar asked Feb 27 '26 04:02

softboxkid


2 Answers

$valid = array_filter($emails, create_function('$s', 'return filter_var($s, FILTER_VALIDATE_EMAIL);'));

Or for PHP 5.3+:

$valid = array_filter($emails, function ($s) { return filter_var($s, FILTER_VALIDATE_EMAIL); });
like image 124
deceze Avatar answered Mar 01 '26 18:03

deceze


<?php
$len=count($array);
for ($i=0;$i<$len;$i++)
  if (preg_match('^[a-z0-9!#$%&*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+(?:[a-z]{2,4}|museum|travel)$/i',$array[$i]))
      echo $array[$i];
?>
like image 37
Sourav Avatar answered Mar 01 '26 18:03

Sourav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!