note 54121 added to function.preg-match
| From: | (parseme) | Date: | Fri, 24 Jun 2005 10:30:47 +0000 |
| Subject: | note 54121 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90884@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54121
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54121&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54121&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54121&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54121&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54121&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54121&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54121&report=yes
Search -- http://master.php.net/manage/user-notes.php