PHP Filters Advanced
In this page:
$result = filter_var_array($data, ["key" => FILTER_VALIDATE_EMAIL, "key2" => FILTER_VALIDATE_INT]);
$result = filter_var($value, FILTER_CALLBACK, ["options" => "function_name"]);
$result = filter_var($value, FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_THOUSAND);
Filtering an Entire Array with filter_var_array()
filter_var_array($data, $rules) applies a different filter to every key in an array in one call, following a $rules array that maps each key name to the filter it should use -- much more convenient than calling filter_var() separately for every single form field.
Example: Filtering an Entire Array with filter_var_array()
<?php
// Declare `$data` as an array: `["email" => "[email protected]", "age" => "25"]`
$data = ["email" => "[email protected]", "age" => "25"];
// Declare `$rules` as an array: `["email" => FILTER_VALIDATE_EMAIL, "age" => FILTER_VALIDATE_INT]`
$rules = ["email" => FILTER_VALIDATE_EMAIL, "age" => FILTER_VALIDATE_INT];
// Print a human-readable dump of `filter_var_array($data, $rules)`
print_r(filter_var_array($data, $rules));
?>
Login to try C/C++/Java/PHP code in the editor
Writing a Custom Filter with FILTER_CALLBACK
FILTER_CALLBACK lets you supply your own function as the validation rule, for checks PHP's built-in filters do not directly support -- like confirming a username only contains letters, numbers, and underscores, or that a value matches a business-specific rule.
Example: Writing a Custom Filter with FILTER_CALLBACK
<?php
// Declare `$username`, set to "alice_99"
$username = "alice_99";
$options = ["options" => function ($value) {
// Return `preg_match('/^\w+$/', $value) ? $value : false` from this function
return preg_match('/^\w+$/', $value) ? $value : false;
}];
// Print a detailed dump (with types) of `filter_var($username, FILTER_CALLBACK, $options)`
var_dump(filter_var($username, FILTER_CALLBACK, $options));
?>
Login to try C/C++/Java/PHP code in the editor
Fine-Tuning Filters with Flags
Many filters accept additional flags to adjust their exact behavior -- FILTER_FLAG_ALLOW_THOUSAND lets FILTER_VALIDATE_INT accept thousands separators, and FILTER_NULL_ON_FAILURE changes a boolean filter to return null (instead of false) specifically for unrecognized input, distinguishing it from a genuine "false" value.
Example: Fine-Tuning Filters with Flags
<?php
// Print a detailed dump (with types) of `filter_var("1,000", FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_THOUSAND)`
var_dump(filter_var("1,000", FILTER_VALIDATE_INT, FILTER_FLAG_ALLOW_THOUSAND));
// Print a detailed dump (with types) of `filter_var("maybe", FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)`
var_dump(filter_var("maybe", FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
?>
Login to try C/C++/Java/PHP code in the editor
Sanitizing Multiple Fields at Once
filter_var_array() works for sanitization filters too, not just validation ones -- applying FILTER_SANITIZE_STRING-style cleanup or FILTER_SANITIZE_NUMBER_INT across an entire array of submitted fields in one call, cleaning up several inputs simultaneously.
Example: Sanitizing Multiple Fields at Once
<?php
// Declare `$data` as an array: `["age" => "25abc", "phone" => "555-1234"]`
$data = ["age" => "25abc", "phone" => "555-1234"];
// Declare `$rules` as an array: `["age" => FILTER_SANITIZE_NUMBER_INT, "phone" => FILTER_SANITIZE_NUMBER_INT]`
$rules = ["age" => FILTER_SANITIZE_NUMBER_INT, "phone" => FILTER_SANITIZE_NUMBER_INT];
// Print a human-readable dump of `filter_var_array($data, $rules)`
print_r(filter_var_array($data, $rules));
?>
Login to try C/C++/Java/PHP code in the editor
Filtering Superglobal Input Directly
filter_input(INPUT_GET, "key", $filter) or filter_input(INPUT_POST, "key", $filter) reads and filters a value directly from the $_GET or $_POST superglobals in one step, without touching the raw superglobal array at all -- a slightly safer, more explicit pattern than reading and then separately filtering.
Example: Filtering Superglobal Input Directly
<?php
// Set `$_GET['id']` to "42"
$_GET['id'] = "42";
// Declare `$id`, set to `filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT)`
$id = filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT);
// Print a detailed dump (with types) of `$id`
var_dump($id);
?>
Login to try C/C++/Java/PHP code in the editor
- Forgetting that filter_var() returns false on failure for most filters, which is itself a valid boolean value -- always compare with === false rather than a loose truthy check.
- Not realizing FILTER_VALIDATE_INT and similar filters reject values with leading/trailing whitespace by default unless the appropriate flag is added.
- Writing a custom FILTER_CALLBACK function that does not itself return false on invalid input, breaking the consistent success/failure contract other filters follow.
- filter_var_array() applies filters to every value in an array at once, following a rules array that maps keys to filter definitions.
- FILTER_CALLBACK lets you supply your own custom validation function for rules PHP's built-in filters do not cover.
- Filter flags like FILTER_FLAG_ALLOW_THOUSAND or FILTER_NULL_ON_FAILURE fine-tune exactly how a filter behaves.
The full PHP filter extension, including filter_var_array() and FILTER_CALLBACK, has been available since PHP 5.2.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: