Remove invalid email format in PHP

两盒软妹~` 提交于 2019-12-06 04:28:59
<?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];
?>
$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); });

Basic solution is simple and doesn't require regex

  $isvalid = filter_var('myname@anydomain.com', FILTER_VALIDATE_EMAIL));

But...

One line solution just doesn't exist (using regex or something else).

To learn more about valid e-mail addresses, follow references below.

References:

Some valid examples are:

  • 甲斐@黒川.日本
  • Rδοκιμή@παράδειγμα
  • Pelé@example.com
  • jsmith@[192.168.2.1]
  • abc\@xyz@domain.com

Those addreses are not compilant with RFC 5322 standard, but they are still valid.

Even you validate some addresses as correct, nothing can guarantee that your e-mail server will accept them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!