2011-02-14 97 views
1

我正在根据表单值创建一个正则表达式数组,并使用错误的用户输入失败的函数。我每次运行的网站,我收到以下错误:使用正则表达式数组进行PHP验证

警告: eregi()[function.eregi]:REG_EMPTY

我不知道什么是错。请看看我的代码和帮助。谢谢!

$error_log = array(); 
// Checks if user inputed data matches the default values 
$arr = array( 
     'name' => 'First Name Last Name', 
     'month' => 'MM', 
     'day' => 'DD', 
     'year' => 'YYYY', 
     'address1' =>'Address Line 1', 
     'address2' => 'Address Line 2', 
     'email' => '[email protected]' 
     ); 

$regex = array(
     'name' => "^[a-z .'-]+$", 
     'month' => "^((0[1-9])|(1[0-2]))$ ", 
     'day' => "0?[1-9]|[1-2][0-9]|3[0-1]", 
     'year' => "^(19|20)\d{2}$", 
     'address1' => "/^[a-zA-Z0-9 ]*$/", 
     'address2' => "/^[a-zA-Z0-9 ]*$/", 
     'email' => "^[A-Z0-9._%-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$" 
     ); 
/* 
Runs validation on the form values and stops procesing if the form does not have the correct values 
*/ 
function regexValidate($form_value, $regex, $key){ 
    if(!eregi($regex[$key],$form_value)){ 
     return true;  
    } 
    return false; 
} 
+3

*(阿里纳斯)就可以扩大*由于PHP的5.3.0,`eregi`不赞成使用[PCRE扩展](http://de2.php.net/manual/en/book.pcre.php)。调用这个函数将发出一个`E_DEPRECATED`通知。查看[差异列表](http://de2.php.net/manual/en/reference.pcre.pattern.posix.php)以获得转换为PCRE的帮助。 – Gordon 2011-02-14 16:40:04

+1

我建议你尝试输出`$ key`和`$ regex [$ key]`来查看你正在尝试使用哪个正则表达式。如果在那里有问题(比如说,一个意外的键值),你将得到一个null regex馈给eregi(),导致你的错误。正如戈登指出的那样,eregi()已被弃用。我认为preg_match()将是一个更好的选择。 – 2011-02-14 16:40:12

回答

2

我有一个稍微不同的方法。我有一个这样的数组 - 我最终传递给Smarty来构建我的for - 但是在提交时,这个数组传递给$ _POST成一个循环的函数,并进行必要的验证(基于此处的第二个参数);

$arrFields = array(
       'stage1' => array(
        'accountname'   => array('Account Name','text','text','stage1',true), 
        'presharedaccountkey' => array('Pre-shared Key','text','text','stage1',true), 
        'ipwhitelist'   => array('IP White-list','text','text','stage1',false), 
        'accountid'    => array('Customer ID','text','readonly','stage1',false), 
        'product'    => array('Product','text','select','stage1',false,$platformProducts), 
        'dtcreated'    => array('Created on','text','text','stage1',false), 
        'createdby'    => array('Created By','text','text','stage1',false), 
        'mode'     => array('Mode','text','radio','stage1',true,$platformModes) 
       ) 
      ); 

我做一些定制的东西,但基本上是循环它会是:

function validateFormPost($arrFields,$postfields) 
{ 
    $err = array(); 
    //validate required fields 
    foreach($arrFields as $stagekey => $stageval) 
    { 
     foreach($stageval as $key => $val) 
     { 
      // your validation based on field type 
      // i.e. in here would be regex to deal with std type or indeed you could add it as a parameter to the $arrFields construct. 


      // allow comma or spaced emails but replace with semi colon 
    if($val[1]=='email') 
       { 
        $postfields[$key] = str_replace(",",";",trim($postfields[$key])); 
        $postfields[$key] = str_replace(" ",";",trim($postfields[$key])); 
       } 
       // basic check required fileds are completed 
       if($val[4]==true && !array_key_exists($key,$postfields)) 
       { 
        $err[$stagekey][$key] = array($val[0],'This is a required field'); 
       } 
       elseif(array_key_exists($key,$postfields) && $this->validateField($postfields[$key],$val[1],$val[4])==false) 
       { 
        $err[$stagekey][$key] = array($val[0],'Invalid '.$val[1].' value.'); 
       //check values for basic field length 
       } 
       elseif(strlen($postfields[$key])>$feildcolset[$key][0] && $feildcolset[$key][0]!=null) 
       { 
        $err[$stagekey][$key] = array($val[0],'Field max '.$feildcolset[$key][0].' characters.'); 
       } 

     } 
    } 
} 

HTH - 我可以在需要:)