【发布时间】:2017-03-09 07:39:46
【问题描述】:
我使用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()将值放入数组中,然后检查数组的约束。这要容易得多。 -
这是stackoverflow.com/questions/42679522/… 的类似故事,@AbraCadaver 提供了解决方案。你应该学习和使用
parse_str函数 -
@axiac 我想你是对的。我来看看这个函数。
-
@RomanPerekhrest 很遗憾,因为他后来删除了他的答案。
标签: php preg-match preg-match-all