note 68291 deleted from function.preg-match by colder

From: Date: Mon, 18 Jun 2007 18:31:57 +0000
Subject: note 68291 deleted from function.preg-match by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-127631@lists.php.net to get a copy of this message
Note Submitter: WK Group ---- 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]); } ?>

« previous php.notes (#127631) next »