9

PHP Manual: filter_var_array()

mixed filter_var_array ( array $data [, mixed $definition [, bool $add_empty = true ]] )

Can something like this for the $definition argument of a filter_var_array() call ever work? (array syntax >= PHP5.4)

   $def = [
         'firstName' => ['filter' => FILTER_SANITIZE_STRING,
                         'flags'  => [FILTER_REQUIRE_SCALAR | FILTER_FLAG_NO_ENCODE_QUOTES | FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH]],
         'lastName'  => ['filter' => FILTER_SANITIZE_STRING,
                         'flags'  => [FILTER_REQUIRE_SCALAR | FILTER_FLAG_NO_ENCODE_QUOTES | FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH]],
   ];

Is using only one flag at a time the only way to use flags when using filter_input_array()? For example: (written out the long way, although I could do this with a loop).

   $def1 = [
         'firstName' => ['filter' => FILTER_SANITIZE_STRING,
                         'flags'  => FILTER_REQUIRE_SCALAR],
         'lastName'  => ['filter' => FILTER_SANITIZE_STRING,
                         'flags'  => FILTER_REQUIRE_SCALAR]
   ];

   $def2 = [
         'firstName' => ['filter' => FILTER_SANITIZE_STRING,
                         'flags'  => FILTER_FLAG_NO_ENCODE_QUOTES],
         'lastName'  => ['filter' => FILTER_SANITIZE_STRING,
                         'flags'  => FILTER_FLAG_NO_ENCODE_QUOTES]
   ];

   $def3 = [
         'firstName' => ['filter' => FILTER_SANITIZE_STRING,
                         'flags'  => FILTER_FLAG_STRIP_LOW],
         'lastName'  => ['filter' => FILTER_SANITIZE_STRING,
                         'flags'  => FILTER_FLAG_STRIP_LOW]
   ];

   $def4 = [
         'firstName' => ['filter' => FILTER_SANITIZE_STRING,
                         'flags'  => FILTER_FLAG_STRIP_HIGH],
         'lastName'  => ['filter' => FILTER_SANITIZE_STRING,
                         'flags'  => FILTER_FLAG_STRIP_HIGH]
   ];
1
  • This probably works if the 'flags' are not inside their own array, as in the first code block in this question. Testing. Commented May 11, 2015 at 19:02

1 Answer 1

9

Just make sure you do not put the flags inside their own array. Options can be in an array, but the flags need to be seen as one whole thing separated by the bitwise OR operator (|).

   $def = [
      'firstName' => ['filter' => FILTER_SANITIZE_STRING,
                       'flags' => FILTER_REQUIRE_SCALAR | FILTER_FLAG_NO_ENCODE_QUOTES | FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH],
      'lastName'  => ['filter' => FILTER_SANITIZE_STRING,
                       'flags' => FILTER_REQUIRE_SCALAR | FILTER_FLAG_NO_ENCODE_QUOTES | FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH]
   ];
Sign up to request clarification or add additional context in comments.

3 Comments

And what if you want to do FILTER_VALIDATE_EMAIL and FILTER_CALLBACK? Still need to do two of them?
That's right. In fact, I have a class that does FILTER_CALLBACK for sanitizing, then I run my fields through the whole thing again with FILTER_SANTIZE_EMAIL or whatever field specific filter applies. Moreover, I have a second class that handles validation. This time, I let it run its field specific FILTER_VALIDATE_BLAH filter first, then I use FILTER_CALLBACK again to run my custom validation routines. In short, sanitize (custom, php filter stuff) then validate (php filter stuff, custom validators).
Those "pipes" are actually the bitwise OR operator. php.net/manual/en/language.operators.bitwise.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.