2011-02-14 100 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 - 我可以在需要:)