+ -
当前位置:首页 → 问答吧 → “编程红宝书”有奖征集活动第三题

“编程红宝书”有奖征集活动第三题

时间:2008-10-15

来源:互联网

[php]


/**
* 检查E-mail
*
* @param string $email
*
* @return string|boolean
*/
function checkEmail($email)
{
        @preg_match('/^([0-9A-Za-z_\-]+)@([0-9A-Za-z_\-\.]+)\.(\w{2,})$/', $email, $matches);
        if (!isset($matches[0])) {
                return 'E-mail不合法。';
        }
       
        $denyDomain = array('sina', '263', '126', '163', 'qq');

        $matches[2] = strtolower($matches[2]); // 邮箱服务器
        if (in_array($matches[2], $denyDomain)) {
                return '不能使用'.$matches[2].'的邮箱。';
        }
        if (is_numeric($matches[2])) { // 邮箱服务器
                return '邮箱服务器名不能全部为数字。';
        }

        if (is_numeric($matches[1])) { // 邮箱用户名
                return '邮箱用户名不能全部为数字。';
        }

        // 检测邮箱地址是否重复直接查询数据库 参考Discuz的代码
        /*
        $query = $db->query("SELECT uid FROM {$tablepre}members WHERE email='$email' LIMIT 1");
        if ($db->num_rows($query)) {
                return '邮箱地址'.$matches[0].'已经注册过。';
        }
        */

        return true;
}

$email = '[email protected]';
echo (($result = checkEmail($email)) === true) ? 'OK' : $result;

[/php]

作者: zhengyes   发布时间: 2008-10-15