note 43403 rejected from function.ereg by curt
| From: | curt@php.net | Date: | Tue, 06 Jul 2004 03:55:35 +0000 |
| Subject: | note 43403 rejected from function.ereg by curt | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72680@lists.php.net to get a copy of this message | ||
Note Submitter: dave146@burtonsys.com
----
Zach,
You are quite right about the regular expression for validating the syntax of email addresses.
However, some additional improvements are possible.
First, if the regular expression is within 'apostrophes' (which it probably is, if you are
coding in PHP), then the apostrophe in the character class needs to be escaped with a backslash.
Also, unless you use preg_match, the \w character class is unavailable, so you need to specify it
explicitly (letters, digits, and underscore). Also, if you use eregi() then you needn't
explicitly include both a-z and A-Z in a character classes, just one or the other will do. This is
your regular expression, with those three changes:
'^[-^!#$%&\'*+\/=?`{|}~._a-z0-9]+@' .
'[-a-z0-9]+(\.[-a-z0-9]+)+$'
Also, TLDs are all 2-6 characters long (".museum" is the long one). You can enforce that
like this:
'^[-^!#$%&\'*+\/=?`{|}~._a-z0-9]+@' .
'[-a-z0-9]+(\.[-a-z0-9]+)*\.[-a-z0-9]{2,6}$'
Also, the RFCs require that you allow domains to be specified as [bracketed] dotted-quad literal IP
addresses:
'^[-^!#$%&\'*+\/=?`{|}~._a-z0-9]+@' .
'([-a-z0-9]+(\.[-a-z0-9]+)*\.[-a-z0-9]{2,6}|' .
'\[[0-9]+(\.[0-9]+){3}\])$'
This is similar, but it ensures that each quad is 1-3 digits:
'^[-^!#$%&\'*+\/=?`{|}~._a-z0-9]+@' .
'([-a-z0-9]+(\.[-a-z0-9]+)*\.[-a-z0-9]{2,6}|' .
'\[[0-9]{1,3}(\.[0-9]{1,3}){3}\])$'
That much I'm sure about. The last two I'm not so sure about...
First, does anyone know whether or not an optional port number is legal as part of a literal domain?
E.g., can you send an email to user@[209.170.129.106:26] ? If so, then this modification to the
regular expression will permit a domain which is a literal IP address to have an optional port
number:
'^[-^!#$%&\'*+\/=?`{|}~._a-z0-9]+@' .
'([-a-z0-9]+(\.[-a-z0-9]+)*\.[-a-z0-9]{2,6}|' .
'\[[0-9]{1,3}(\.[0-9]{1,3}){3}(\:[0-9]{1,5})?\])$'
Also, I assume that an IPv6 address can also be used as a literal domain? If so, then this regular
expression will permit a domain literal IP address in either IPv4 or IPv6 format, with or without a
port number:
'^[-^!#$%&\'*+\/=?`{|}~._a-z0-9]+@' .
'([-a-z0-9]+(\.[-a-z0-9]+)*\.[-a-z0-9]{2,6}|' .
'\[(([a-f0-9]{0,4}\:|[0-9]{1,3}\.){3,8}[a-f0-9]{0,4})\])$'
-Dave