preg_match使用变量?

时间:2014-01-24 22:30:44

标签: php preg-match

这是我想要尝试做的事情。我想要一种简单的方法来添加新的电子邮件,而无需让我的客户端触及主要代码。

$list= 'gmail|yahoo|aol'; 


$banned = '\b[\w\.-]+@((?!'.$list.').)[\w\.-]+\.\w{2,4}\b';

if ( isset($data['email']) && !preg_match($banned, $data['email']) )
{}

或者也许。做一个指向文本文件的包含?里面列出了哪些域名?


更新

这是我的代码..

//Banned Domains
    $banned = 'mailexpire|spamtrail|0815';
    //


    //Emails    
    $email_list = '/^[\w.-]+@((?!'.$banned.').)[\w.-]+\.\w{2,4}$/';             
    if ( isset($data['email']) && preg_match($email_list, $data['email']) )
    {
        $response->addError('Email Addresses from this domain are not allowed, Please try a different email address', 'email');
    }

    else if ( isset($data['email']) && !preg_match($email_list, $data['email']) )
    {
        $query = SK_MySQL::placeholder("SELECT COUNT(`profile_id`) FROM `".TBL_PROFILE."` WHERE `email`='?'",$data['email']);
        if (SK_MySQL::query($query)->fetch_cell()) {
            $error_msg = SK_Language::text('%forms._fields.email.errors.already_exists');
            $response->addError($error_msg,'email');
        }
    }
    //END

由于某种原因..它没有抓住域名?

1 个答案:

答案 0 :(得分:1)

问题在于你忘记了放置模式分隔符:

$banned = '/^[\w.-]+@((?!' . $list . ').)[\w.-]+\.\w{2,4}$/';