2014-01-15 29 views
1

我絆倒在正則表達式的問題,不知道爲什麼這驗證讓我進入一個正確的字符串後一個錯誤。PHP正則表達式沒有工作,一直給我的錯誤

// City validation 
    if (empty($custom_fields["city_id"])) { 
     $response["error"] = TRUE; 
     $response["response"] = "City field is missing. Please try again."; 
     unset($_POST["s2member_pro_paypal_registration"]["nonce"]); 
    } elseif (!preg_match('^[a-zA-Z]+(?:[\s-]+[a-zA-Z]+)*$', $custom_fields["city_id"])) { 
     $response["error"] = TRUE; 
     $response["response"] = "Invalid City name"; 
     unset($_POST["s2member_pro_paypal_registration"]["nonce"]); 
    } 

我進入了紐約並檢查了http://gskinner.com/RegExr/中的正則表達式。它工作正常,但我提交輸入字符串時出現錯誤。

有人可以幫助我嗎?

回答

1

您需要使用required regex delimiterpreg_match代碼,因爲這一個:

preg_match('/^[a-zA-Z]+(?:[\s-]+[a-zA-Z]+)*$/', $custom_fields["city_id"]) 
+0

這工作。我忘了正則表達式分隔符。 – rolu

+0

不客氣,很高興它解決了。 – anubhava