Php/filter validate email

来自菜鸟教程
跳转至:导航、​搜索

PHP FILTER_VALIDATE_EMAIL过滤器

❮PHP筛选器参考

检查变量$ email是否为有效的电子邮件地址:

 <?php
$email = "john.doe@example.com";

if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  echo("$email is a valid email address");
} else {
  echo("$email is not a valid email address");
}
?>

定义和用法

FILTER_VALIDATE_EMAIL过滤器验证电子邮件地址。

更多例子

下面的示例既净化又验证了电子邮件地址:

例1

首先从$ email变量中删除所有非法字符,然后检查它是否为有效的电子邮件地址:

 <?php
$email = "john.doe@example.com";

// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);


// Validate e-mail
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo("$email is a valid email address");
} else {
    echo("$email is not a valid email address");

 }
?>

❮PHP筛选器参考