检查 $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_var() 函数使用指定的过滤器过滤变量。
filter_var(
var, filtername, options)
Parameter | Description |
---|---|
var | Required. The variable to filter |
filtername | Optional. Specifies the ID or name of the filter to use. Default is FILTER_DEFAULT, which results in no filtering |
options | Optional. Specifies one or more flags/options to use. Check each filter for possible options and flags |
返回值: | 成功则返回过滤后的数据,失败则返回FALSE |
---|---|
PHP 版本: | 5.2+ |
下面的示例同时清理和验证电子邮件地址:
首先删除 $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");
}
?>
亲自试一试 »
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!