我使用以下代码:
foreach ($_POST as $key => $value) {
(preg_match("/^._box_(\d+)_5$/", $key, $matches));
//$prodid[] = $matches[1];
$firephp->log($matches, 'matches');
};
此代码正在处理上一页中的$ _POSTed以下信息:
array(
['clin'] =>
['clinmail'] =>
['quest_3'] =>
['quest_7'] =>
['quest_8'] =>
['quest_9'] =>
['quest_10'] =>
['quest_15'] =>
['quest_16'] =>
['hosp'] => 8
['user'] => 16
['a_box_15_5'] => 2
['a_box_16_5'] => 2
['b_box_1_5'] => '$0.00'
['b_box_29_5'] => 1
)
问题是我得到以下结果:
matches: array()
matches: array()
matches: array()
matches: array()
matches: array()
matches: array()
matches: array()
matches: array()
matches: array()
matches: array()
matches: array()
matches: array('0'=>'a_box_15_5', '1'=>'15')
matches: array('0'=>'a_box_16_5', '1'=>'16')
matches: array('0'=>'b_box_1_5', '1'=>'1')
matches: array('0'=>'b_box_29_5', '1'=>'29')
我不希望它与前11个位置匹配。我只想要实际匹配我正在寻找的结果,在这种情况下,最后四个$ _POST不是preg_match应该做的吗?如何将其限制为仅匹配?
答案 0 :(得分:3)
preg_match()
正常工作:如果没有匹配项,$matches
将为空。但如果没有匹配,你就不会做任何不同的事情,你总是打电话给$firephp->log()
,匹配与否。
preg_match()
如果模式匹配则返回1
,否则返回0
,如果发生错误则返回false
,因此您可以使用它来查看是否存在匹配项,以及然后再拨打$firephp->log()
:
foreach ($_POST as $key => $value) {
if (preg_match('/^._box_(\d+)_5$/', $key, $matches)) {
$firephp->log($matches, 'matches');
}
}
答案 1 :(得分:1)
在记录之前添加检查:
foreach ($_POST as $key => $value) {
(preg_match("/^._box_(\d+)_5$/", $key, $matches));
//$prodid[] = $matches[1];
if(!empty($matches)){
$firephp->log($matches, 'matches');
}
};