<?
/**************************************************************************
*
* validate_email($email,$domains=null,$allowed=true)
*
* Validate an email address, with an optional allow/disallow domain spec.
*
* Parameters:
* $email The FULL email address, e.g. [email protected]
* $domains A single domain or an array of domain names
* $allowed If true, $domains contains an array of domain names
* or a single domain name from which $email MUST come.
* If false, $domains contains an array of domains
* or a single domain from which $email MAY not come.
*
* Returns:
* true or false as the case may be.
*
* The tests are all case INsensitive.
*
* Examples:
*
* validate_email($email); any valid email address is allowed
* validate_email($email,'yahoo.com',true); ONLY email from YAHOO.COM addresses allowed
* validate_email($email,'yahoo.com',false); yahoo.com email addresses are NOT allowed
* validate_email($email,array('yahoo.com','gmail.com'),true); yahoo and gmail ONLY allowed
* validate_email($email,array('yahoo.com','gmail.com'),false); yahoo and gmail are DISALLOWED
**************************************************************************/
function validate_email($email,$domains=null,$allowed=true)
{
if (is_array($domains)) // a list, so flatten it
{
// empty array? default to all domains
if (count($domains) == 0)
$domains = '[-0-9a-z]+[.]+([-0-9a-z]{2,6}+[.]){0,1}[a-z]{2,6}';
else // now we have a regex pattern like domain1|domain2|domain3
$domains = implode('|',$domains);
}
else if (!$domains)
$domains = '[-0-9a-z]+[.]+([-0-9a-z]{2,6}+[.]){0,1}[a-z]{2,6}';
// now check, regardless of whether original input was a list or a string or nothing
return !preg_match('/^([0-9a-z]+[-._+&])*[0-9a-z]+@(' . $domains . ')$/i',$email) ? !$allowed : $allowed;
}
?>