note 89638 added to function.preg-match

From: Date: Tue, 17 Mar 2009 17:23:30 +0000
Subject: note 89638 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-152050@lists.php.net to get a copy of this message
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 ---- Server IP: 212.124.37.9 Probable Submitter: 80.121.51.3 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- https://master.php.net/note/edit/89638 Del: integrated -- https://master.php.net/note/delete/89638/integrated Del: useless -- https://master.php.net/note/delete/89638/useless Del: bad code -- https://master.php.net/note/delete/89638/bad+code Del: spam -- https://master.php.net/note/delete/89638/spam Del: non-english -- https://master.php.net/note/delete/89638/non-english Del: in docs -- https://master.php.net/note/delete/89638/in+docs Del: other reasons-- https://master.php.net/note/delete/89638 Reject -- https://master.php.net/note/reject/89638 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#152050) next »