Current Location: Home> Latest Articles> Clean data with filter_var_array and custom filters

Clean data with filter_var_array and custom filters

gitbox 2025-05-29

In PHP, data filtering and verification are key links in ensuring application security and stability. PHP provides a powerful set of filter functions, where filter_var_array is a very practical tool for processing multidimensional array data. It not only supports a variety of built-in filters, but also allows us to combine custom filtering functions to achieve more accurate and complex data filtering.

This article will explain in detail how to use filter_var_array combined with custom filters to complete efficient filtering of complex data.


1. Introduction to filter_var_array

filter_var_array is used to apply different filtering rules to elements in an array. It receives two parameters:

  • The first parameter is the array to be filtered.

  • The second parameter is an associative array that defines the filter corresponding to each key.

example:

 <?php
$data = [
    'email' => '[email protected]',
    'age' => '25',
];

$filters = [
    'email' => FILTER_VALIDATE_EMAIL,
    'age' => [
        'filter' => FILTER_VALIDATE_INT,
        'options' => ['min_range' => 18, 'max_range' => 99]
    ],
];

$result = filter_var_array($data, $filters);
var_dump($result);

At this point, $result will return the filtered data or false .


2. Use custom filters

The built-in filters meet most needs, but some complex data require custom logic, such as complex verification of string formats, cross-field verification, etc.

filter_var_array supports the implementation of FILTER_CALLBACK combined with custom functions:

 <?php
function custom_filter($value) {
    // Example:Only letters and numbers are allowed,And the length is5arrive10Characters
    if (preg_match('/^[a-zA-Z0-9]{5,10}$/', $value)) {
        return $value;
    }
    return false;
}

$data = [
    'username' => 'User123',
    'email' => 'invalid-email',
];

$filters = [
    'username' => [
        'filter' => FILTER_CALLBACK,
        'options' => 'custom_filter',
    ],
    'email' => FILTER_VALIDATE_EMAIL,
];

$result = filter_var_array($data, $filters);
var_dump($result);

3. Complex examples combining filter_var_array with custom filters

Assuming that there is the following complex data structure, the following filtering rules are required:

  • username : 5-15 alphanumeric numbers, underscore allows, must start with letters.

  • email : standard mailbox format.

  • age : integer between 18-65.

  • website : Must be a legal URL, and the domain name must be gitbox.net .

The sample code is as follows:

 <?php
function validate_username($value) {
    if (preg_match('/^[a-zA-Z][a-zA-Z0-9_]{4,14}$/', $value)) {
        return $value;
    }
    return false;
}

function validate_website($url) {
    // Verify firstURLFormat
    if (!filter_var($url, FILTER_VALIDATE_URL)) {
        return false;
    }
    // Resolve the domain name part
    $host = parse_url($url, PHP_URL_HOST);
    // The domain name must be gitbox.net
    if ($host === 'gitbox.net') {
        return $url;
    }
    return false;
}

$data = [
    'username' => 'User_01',
    'email' => '[email protected]',
    'age' => '30',
    'website' => 'https://gitbox.net/project',
];

$filters = [
    'username' => [
        'filter' => FILTER_CALLBACK,
        'options' => 'validate_username',
    ],
    'email' => FILTER_VALIDATE_EMAIL,
    'age' => [
        'filter' => FILTER_VALIDATE_INT,
        'options' => ['min_range' => 18, 'max_range' => 65],
    ],
    'website' => [
        'filter' => FILTER_CALLBACK,
        'options' => 'validate_website',
    ],
];

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

if ($result === false || in_array(false, $result, true)) {
    echo "Data verification failed\n";
} else {
    echo "Data verification passed\n";
    var_dump($result);
}

4. Summary

  • filter_var_array is a powerful tool for processing batch data filtering.

  • Built-in filters are suitable for most standard data types.

  • FILTER_CALLBACK can be combined with custom functions to achieve accurate verification of complex rules.

  • When combined with URL verification, the domain name part can be customized filtered using parse_url .

Mastering this set of techniques can greatly improve the security and accuracy of PHP applications when receiving and processing complex user input data.