note 68291 added to function.preg-match

From: Date: Fri, 21 Jul 2006 00:34:23 +0000
Subject: note 68291 added to function.preg-match
Groups: php.notes 
Request: Send a blank email to php-notes+get-114992@lists.php.net to get a copy of this message
In spite of the note on the page with the posting form saying that my regex is 'almost certainly wrong,' I still think it may be worth to post it here :-) The regex from the earlier message has a few defects (according to the email syntax described in RFC 3696): - permits the local part to end with a dot, - the size of the local part is not restricted, - the length of the domain part is checked incorrectly (eg, allows up to 70 characters instead of 64), - doesn't allow a dot at the very end. I'd like to suggest the following function for validating emails (though I'm not sure whether or not neighbouring hyphens are allowed in domain part): <?php function is_email($email){ $x = '\d\w!\#\$%&\'*+\-/=?\^_`{|}~'; //just for clarity return count($email = explode('@', $email, 3)) == 2 && strlen($email[0]) < 65 && strlen($email[1]) < 256 && preg_match("#^[$x]+(\.?([$x]+\.)*[$x]+)?$#", $email[0]) && preg_match('#^(([a-z0-9]+-*)?[a-z0-9]+\.)+[a-z]{2,6}.?$#', $email[1]); } ?> ---- Server IP: 195.136.184.34 Probable Submitter: 83.22.42.189 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match.php Edit -- http://master.php.net/note/edit/68291 Del: integrated -- http://master.php.net/note/delete/68291/integrated Del: useless -- http://master.php.net/note/delete/68291/useless Del: bad code -- http://master.php.net/note/delete/68291/bad+code Del: spam -- http://master.php.net/note/delete/68291/spam Del: non-english -- http://master.php.net/note/delete/68291/non-english Del: in docs -- http://master.php.net/note/delete/68291/in+docs Del: other reasons-- http://master.php.net/note/delete/68291 Reject -- http://master.php.net/note/reject/68291 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#114992) next »