我正在使用PHP 5.3.10版本。以下是程式碼:
<?php $email = "test@example.c"; if (filter_var($email, FILTER_VALIDATE_EMAIL)) echo "Email: ".$email." correct"; else echo "email not correct"; ?>
它回傳:"Email: test@example.c正確。
我認為只有一個字元的頂級網域是不正確的(根據這個列表:http://data.iana.org/TLD /tlds-alpha-by-domain.txt,我不知道有一個字元長度的頂級網域)。
那麼,FILTER_VALIDATE_EMAIL過濾器是否正常運作?
FILTER_VALIDATE_EMAIL不支援PHP 5.2.14
驗證電子郵件地址有點複雜。看看這個列表:
有效的電子郵件地址
無效的電子郵件地址-
- Abc.example.com (an @ character must separate the local and domain
parts)
- A@b@c@example.com (only one @ is allowed outside quotation marks)
- a"b(c)d,e:f;gi[j\k]l@example.com (none of the special characters
in this local part are allowed outside quotation marks)
- just"not"right@example.com (quoted strings must be dot separated, or
the only element making up the local-part)
- this is"not\allowed@example.com (spaces, quotes, and backslashes may
only exist when within quoted strings and preceded by a backslash)
this\ still"not\allowed@example.com (even if escaped (preceded by a backslash), spaces, quotes, and backslashes must still be contained by quotes)Source
http://en.wikipedia.org/wiki/Email_address#大多數所有的電子郵件驗證實作都是“bug”,但php實作可以很好地使用,因為它接受所有常見的電子郵件地址
更新: Found on
http://www.php.net/manual/en/filter.filters.validate.php#關於沒有的「部分」位址。在網域部分,原始碼中的一條註解(在ext/filter/logical_filters.c中)證明了這種拒絕:
###這裡是Michael Rushton的類別連結(連結已斷,請參閱下面的原始程式碼),它支援RFC 5321/5322###