如何对所有POST数据运行PHP Preg_match?

如何对所有POST数据运行PHP Preg_match?,php,preg-match,postdata,Php,Preg Match,Postdata,我有一个HTML表单,用户可以在表单中提交信息,这取决于用户在表单中选择了多少选项发送了不同数量的POST数据,有些是作为数组发送的 使用VARDUMP时,POST数据如下所示: array(8) { ["animal"]=> string(7) "test123" ["name"]=> string(5) "test" ["goat"]=> string(7) "farm" ["animal2"]=> string(8) "animal2" ["option"]=>

我有一个HTML表单,用户可以在表单中提交信息,这取决于用户在表单中选择了多少选项发送了不同数量的POST数据,有些是作为数组发送的

使用VARDUMP时,POST数据如下所示:

array(8) { ["animal"]=> string(7) "test123" ["name"]=> string(5) "test" ["goat"]=> string(7) "farm" ["animal2"]=> string(8) "animal2" ["option"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["number"]=> array(2) { [0]=> string(1) "s" [1]=> string(1) "3" } ["option4"]=> array(1) { [0]=> string(1) "1" } ["grass"]=> array(1) { [0]=> string(1) "3" } }
我希望根据所有POST数据运行我的preg_匹配:

if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $data)) || (preg_match("/\\s/", $data))
{
    exit("Illegal characters found");
}
如何实现这一点?

您可以使用迭代遍历整个$\u POST数组,用代码测试每个值:


在preg_match中,您有两个不同的变量,您想根据每个POST值测试两个正则表达式吗?@Nick是的,这是正确的,或者仅仅是一个例子就足够了。我已经更新了它,使它更清晰了。为什么要调用preg_match两次,而不是将两个模式都作为替代项放在正则表达式中呢?preg_match'/[\'^$%&*}{@~?>,[124;=+-\\ s]/',$data
array_walk_recursive($_POST, function ($v) {
    if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $v) || preg_match("/\\s/", $v)) {
        exit("Illegal characters found");
    }
});