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]
];
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]
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With