0

我正在使用以下代码:

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 应该做的吗?我怎样才能将它限制在比赛中?

4

2 回答 2

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');
    }
}
于 2012-11-23T20:40:33.250 回答
1

在记录之前添加检查:

foreach ($_POST as $key => $value) {
    (preg_match("/^._box_(\d+)_5$/", $key, $matches));
    //$prodid[] = $matches[1];
    if(!empty($matches)){
        $firephp->log($matches, 'matches');
    }
};
于 2012-11-23T20:40:31.043 回答