Jonny P
Jonny P

Reputation: 453

How can i run PHP Preg_match against all POST Data?

I have a HTML form where users submit information, depending how many options the user selects in a form there is a different number of POST data sent, some as an array.

The POST data looks like this with VARDUMP:

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" } }

I wish to run my preg_match against all POST data:

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

How can i achieve this?

Upvotes: 0

Views: 324

Answers (1)

Nick
Nick

Reputation: 147216

You could use array_walk_recursive to iterate over the entire $_POST array, testing each value with your preg_match code:

array_walk_recursive($_POST, function ($v) {
    if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $v) || preg_match("/\\s/", $v)) {
        exit("Illegal characters found");
    }
});

Upvotes: 1

Related Questions