示例 #1 清理和验证电子邮件地址
<?php
$a = '[email protected]';
$b = 'bogus - at - example dot org';
$c = '([email protected])';
$sanitized_a = filter_var($a, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_a, FILTER_VALIDATE_EMAIL)) {
echo "这个 (a) 清理后的电子邮件地址被认为是有效的。\n";
}
$sanitized_b = filter_var($b, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_b, FILTER_VALIDATE_EMAIL)) {
echo "这个清理后的电子邮件地址被认为是有效的。";
} else {
echo "这个 (b) 清理后的电子邮件地址被认为是无效的。\n";
}
$sanitized_c = filter_var($c, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_c, FILTER_VALIDATE_EMAIL)) {
echo "这个 (c) 清理后的电子邮件地址被认为是有效的。\n";
echo "之前:$c\n";
echo "之后:$sanitized_c\n";
}
?>
以上示例将输出
This (a) sanitized email address is considered valid. This (b) sanitized email address is considered invalid. This (c) sanitized email address is considered valid. Before: ([email protected]) After: [email protected]