note 89638 deleted from function.preg-match by danbrown
| From: | danbrown@php.net | Date: | Tue, 17 Mar 2009 17:25:41 +0000 |
| Subject: | note 89638 deleted from function.preg-match by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152051@lists.php.net to get a copy of this message | ||
Note Submitter: thomas at redefine dot at
Reason: bad code
----
Yet another email validation trail...
I just checked out some approaches to do simple e-mail validation and tried to think that out
straight as simple and rational as possible!
So mails can be like what? Text at the beginning and then ( a DOT or an UNDERLINE or a HYPHEN
followed by text ) * whereas the last two can be repeated sometimes! That is the generall form
before the @ sign in an e-mail adress and after that it is quite the same untill the TLD(.com, .at,
.whatever...). So, basically it should be easy to do this!
And here is.. my own little "not all to complex" approach:
<?php
$pattern = "/".
"[a-z0-9]+".
"([. - _]? [a-z0-9]+)*".
"\@".
"[a-z0-9]+".
"([. - _]? [a-z0-9]+)*".
"\.".
"[a-z]{2,4} $
/sx";
1) a character from a-z (at least once)
2) a group
2.1. a DOT, a HYPHEN or an UNDERLINE (0 or 1 times)
2.2. a character a-z or a numeric from 0-9 (at least once)
3) the @ sign
4 & 5 == 1 & 2
6) the DOT before the TLD
7) The TLD can be ".com", ".name",.... and so on whereas {2,4} means at least
two characters in length but not more than four
8) s and x is for ignoring whitespace, comments, and treaded as a single line
$email = "me_and-you@go.wild-together.com";
preg_match($pattern, $email);
?>
feel free to correct or improve me and my naive young mind!
regards Tom