【发布时间】:2019-01-22 06:34:47
【问题描述】:
我在玩 filter_var 来验证电子邮件:
$email = "test@test.com";
if( filter_var( $email, FILTER_VALIDATE_EMAIL ) ){
echo "{Valid Email";
}else{
echo "Invalid Email";
}
返回Valid Email。
但是当我在本地尝试像. $ # * ? & ! ^ ~ / ' - _ @ % {} | = +这样的特殊字符时,像:
$email = "te?st@test.com";
$email = "te/st@test.com";
$email = "te'st@test.com";
$email = "te}st@test.com";
$email = "te|st@test.com";
$email = "te=st@test.com";
这也会返回Valid Email。
它不接受像<> [] , 这样的字符。
但是如果我使用$:
$email = "te$st@test.com";
这将返回Valid Email,但也会返回NOTICE Undefined variable: st
那么在将电子邮件地址插入数据库之前,我应该使用它来验证电子邮件地址吗?
或者我应该使用自定义正则表达式?
【问题讨论】:
-
电子邮件的官方 RFC 非常复杂、冗长,并且包含许多奇怪的东西。查看en.wikipedia.org/wiki/Email_address#Examples 以获取其他有效电子邮件。 In short, do not attempt this with regex because it's already been done and you probably don't know what you are doing. 在您的示例中,
te$st@test.com被插值为te@test.com,因为$st未定义,因此在其位置使用 null(空)并且您会收到警告。
标签: php email-validation