2013-03-19 50 views
-1

我不是一名鐵桿程序員,但我對編碼有一點了解。已棄用:函數ereg()in self built PHP webshop

但是我確實遇到了下面的代碼有問題。 此代碼用於後端表單以更改客戶設置。

public static function validate($array) 
{ 
    if (isset($array[ 'initialen' ]) && trim($array[ 'initialen' ]) != '') 
    { 
     $pattern = '([[:digit:]]|[~`[email protected]#$%^&*()_=+{}|\:;"/?,]|[|]|-)+'; 

     if (ereg($pattern, $array['initialen'])) 
     { 
      $errors[] = 'Initialen incorrect, alleen letters en punten'; 
     } 
     else 
     { 
      $_SESSION['beheer']['klanten']['temp']['initialen'] = strtoupper($array[ 'initialen' ]); 
     } 
    } 
    else 
    { 
     $errors[] = 'Initialen niet ingevuld'; 
    } 

我得到的錯誤:

Deprecated: Function ereg() is deprecated in class_klanten.php on line 138

如果我更改爲:

if (ereg($pattern, $array['initialen'])) 

if (preg_match($pattern, $array['initialen'])) 

我得到以下錯誤:

Warning: preg_match() [function.preg-match]: Unknown modifier '+' in class_klanten.php on line 138

我該如何解決這個問題?

BR,

史蒂夫

回答

0

來源:PHP ereg vs. preg

預浸料是Perl兼容的正則表達式庫 額日格是POSIX complient regex庫

基本模式應有所不同,去http://www.regular-expressions.info/他們有一個很好/完整的參考。

+0

嗨,大家好, 謝謝回答這麼快。 我得到了錯誤信息去其他的PHP文件,但我不明白如何設置在這段代碼中的分隔符: $ pattern ='([[:digit:]] | [〜'!@ #$%^&*()_ = + {} | \ :;「/ ?,] | [|] | - )+'; – Steve 2013-03-19 08:49:33

+0

@Steve'$ pattern ='#([[:digit:]] | [〜'!@ \#$%^&*()_ = + {} | \:;「/ ?,] | [|] | - )+#';' – HamZa 2013-03-19 08:51:26

0

正如錯誤消息所示,ereg已棄用,並且您在preg_match上使用的語法錯誤。

你的代碼更改爲:

$pattern = '#([\d]|[~`[email protected]#$%^&*()_=+{}|\:;"/?,]|[|]|-)+#i' 
if (preg_match($pattern, $array['initialen'])) 

Official document about PCRE pattern

+0

嗯, 改變它,得到錯誤: '警告:preg_match()[function.preg-match]:未知的修飾符'$' – Steve 2013-03-19 08:54:13

+0

啊, 停止印刷機! 它的工作原理。 #([[:digit:]] | [〜!@ \#$%^&*()_ = + {} | \ :;「/ ?,] | [|] | - )+# 謝謝HamZa DzCyber​​DeV – Steve 2013-03-19 09:04:53

+0

並且謝謝路人,欺騙和任意迴應。 保持良好的工作;-) – Steve 2013-03-19 09:08:51