note 54121 deleted from function.preg-match by colder

From: Date: Mon, 18 Jun 2007 18:23:31 +0000
Subject: note 54121 deleted from function.preg-match by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-127606@lists.php.net to get a copy of this message
Note Submitter: MKP dev a.t g! mail d0t com (parseme) ---- Domain name parsing is tricky with an RE. The simplest, most efficient method, is probably just to split the domain by . and individually verify each part, like so: <?php $email_address = 'tom@rocks.my.socks.tld'; // An RE subject function verify_addr ($address) { $return = false; if (preg_match ('/^[\w.]+@([\w.]+)\.[a-z]{2,6}$/i', $address, $domain)) { $domain = explode ('.', $domain[0]); // Split the domain into sections wherein the last element is either 'co', 'org', or the likes, or the primary domain name foreach ($domain as $part) { // Iterate through the parts if (substr ($part, 0, 1) == '_' || substr ($part, strlen ($part) - 1, 1) == '_') $return = false; // The first or last character is _ else $return = true; // The parts are fine. The address seems syntactically valid } } return $return; } if (verify_addr ($email_address)) { // The address is good, what now? } else { // Address is invalid... Do something. } ?> An alternative would be to look for _. and ._ in the domain section, or to just ignore that restriction entirely and use this RE: /^[\w.]+@(?:[\w.]{2,63}\.)+[a-z]{2,6}$/i

« previous php.notes (#127606) next »