start learning
Image 1
51

PHP Advanced Filters

PHP advanced filters typically refer to the filter_var_array and filter_input_array functions. These functions allow you to apply multiple filters to an array of data in a concise manner, providing a powerful way to validate and sanitize data in bulk.

Let's explore these advanced filters with examples and clarifications :

filter_var_array example

Sanitization and Validation

$data = array(
    "name" => "John Doe",
    "email" => "john@example.com",
    "age" => "25",
    "website" => "http://www.example.com",
    "ip_address" => "192.168.0.1"
);

$filters = array(
    "name" => array("filter" => FILTER_SANITIZE_STRING),
    "email" => array("filter" => FILTER_VALIDATE_EMAIL),
    "age" => array("filter" => FILTER_VALIDATE_INT, "options" => array("min_range" => 1, "max_range" => 120)),
    "website" => array("filter" => FILTER_VALIDATE_URL),
    "ip_address" => array("filter" => FILTER_VALIDATE_IP)

);

$filtered_data = filter_var_array($data, $filters);

var_dump($filtered_data);
 

Custom Filter function for Validating and Sanitizing

$data = array(
function customFilter($value) {
    // Add your custom validation/sanitization logic
    $filteredValue = /* ... */;
    return $filteredValue;
}

$userInput = "custom_input";

$filteredInput = filter_var($userInput, FILTER_CALLBACK, ['options' => 'customFilter']);

echo "Original Input: $userInput<br>";
echo "Filtered Input: $filteredInput";
 

The provided code defines a custom filter function (customFilter) and applies it to a sample input ($userInput) using filter_var with FILTER_CALLBACK, allowing you to add your own validation or sanitization logic.


filter_input_array Example

$filters = array(
    "name" => FILTER_SANITIZE_STRING,
    "email" => FILTER_VALIDATE_EMAIL,
    "age" => array("filter" => FILTER_VALIDATE_INT, "options" => array("min_range" => 1, "max_range" => 120)),
    "website" => FILTER_VALIDATE_URL,
    "ip_address" => FILTER_VALIDATE_IP
);

$input_data = filter_input_array(INPUT_POST, $filters);

var_dump($input_data);

Example: Validate and Sanitize Input from Form using filter_input():

Assuming you have a form with a field named user_input submitted using the POST method:

<!-- HTML Form -->
<form method="post" action="">
    <label for="user_input">User Input:</label>
    <input type="text" name="user_input" id="user_input">
    <input type="submit" value="Submit">
</form>
Now, in your PHP script:
<?php

// Get and sanitize input from the form
$userInput = filter_input(INPUT_POST, 'user_input', FILTER_SANITIZE_STRING);

// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // Validate the input
    if (filter_var($userInput, FILTER_VALIDATE_INT)) {
        echo "Valid integer: $userInput";
    } else {
        echo "Invalid input";
    }
}

?>

These advanced filtering techniques with filter_var_array and filter_input_array are powerful tools for processing data efficiently in a structured way. They are particularly useful when dealing with form submissions or batches of data.