我正在使用preg_match_all
以確保字符串遵循特定模式。爲什麼我的preg_match_all不能正常工作?
由於字符串遵循該模式,它應該顯示'所有條件都滿足',但是它會顯示'條件淨滿足'。
$order = "item[]=2&item[]=1&item[]=3&item[]=4&item[]=5&item[]=6&item[]=7&item[]=8&item[]=9&item[]=10&item[]=11&item[]=12";
$pattern = "/^(item\[\]=([1-9]|10|11|12))(&(item\[\]=([1-9]|10|11|12))){11}$/";
if(preg_match($pattern, $order)) {
// check for repetition
$matches = [];
preg_match_all("/\d+/", $order, $matches);
if(count(array_count_values($matches[0])) == 12) {
// All are unique values
echo 'All conditions met';
}
}else{
echo 'Conditions not met';
}
您的'$ pattern'正則表達式不完整,您發佈了正在使用的正則表達式嗎? –
您的輸入字符串看起來像查詢字符串。我會使用['parse_str()'](http://php.net/manual/en/function.parse-str.php)將值放入一個數組,然後檢查數組的約束。這很容易。 – axiac
這是一個類似的故事http://stackoverflow.com/questions/42679522/make-sure-that-string-follows-the-required-format並從@AbraCadaver有解決方案。你應該學習和使用'parse_str'函數 – RomanPerekhrest