PHP filter_list(): List Available Filters

PHP filter_list Function

PHP introduces the filter_list() function to give developers a way to check all available filters in the filter extension.

This helps you ensure compatibility. It allows dynamic input validation and promotes secure data processing without custom filter methods.

In this article, we will cover the following topics:

  • What is the filter_list and how does it work?
  • How to improve input validation.
  • The difference between filter_list and filter_id.
  • Examples.

Let’s get started with how it works and its syntax.

Understand the filter_list Function in PHP

The filter_list() function in PHP is used to return a list of all the supported filters available in the PHP environment. Filters are used to sanitize or validate data, and this function helps you see the available ones.

Here is the syntax:

filter_list();

Here is a quick example:

$filters = filter_list();
foreach ($filters as $filter) {
    echo $filter . "<br>";
}

How it works:

  • It does not take any parameters.
  • It returns an array of filter names that are supported by PHP.
  • You can use these filters with functions like filter_var() or filter_input() to filter or validate data.

So, Why Use filter_list()?

  • That makes sure that you’re using available filters.
  • Helps dynamically select filters.
  • Improves security and prevents invalid input.

Use filter_list to Improve Input Validation in PHP

You can use filter_list() to check available filters and apply them to the user input for better validation in PHP. Here’s how:

$filters = filter_list();
print_r($filters);

Output:

Array
(
[0] => int
[1] => boolean
[2] => float
[3] => validate_regexp
[4] => validate_domain
[5] => validate_url
[6] => validate_email
[7] => validate_ip
[8] => validate_mac
[9] => string
[10] => stripped
[11] => encoded
[12] => special_chars
[13] => full_special_chars
[14] => unsafe_raw
[15] => email
[16] => url
[17] => number_int
[18] => number_float
[19] => add_slashes
[20] => callback
)

Use filter_var() and filter_input() with available filters to validate user input. For example:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);
    $age = filter_input(INPUT_POST, "age", FILTER_VALIDATE_INT);

    if ($email && $age !== false) {
        echo "Valid email and age.";
    } else {
        echo "Invalid input.";
    }
}

In this example, if you pass $_POST with email and age, it should check if $_POST['age'] is an integer and $_POST['email'] is a valid email. If both are valid, it will print ‘Valid email and age’.

You need to take a look at these two articles on global variables: $_POST and $_SERVER to understand how they work.

Instead of hardcoding filter names, check available filters and apply them dynamically:

$available_filters = filter_list();

if (in_array("email", $available_filters)) {
    $email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
    echo $email ? "Valid Email" : "Invalid Email";
}

If you pass a valid email to $_POST, the output will be like this:

Valid Email

Let’s move on to the next section to understand the difference between filter_list and filter_id.

Understand the Difference Between filter_list and filter_id in PHP

Here is a table that shows you the different purposes of input filtering.

FunctionPurposeReturns
filter_list()Lists all available filtersArray of filter names
filter_id($name)Gets the ID of a filterInteger (filter ID)

Use cases:

  • Use filter_list() to check available filters.
  • Use filter_id() to get filter IDs for dynamic filtering.

Wrapping Up

In this article, you learned about the filter_list() function in PHP and its role in input validation and data sanitization. Here’s a quick recap:

  • filter_list() returns all available filters in PHP. It helps developers to check supported options.
  • Using filter_list() makes sure that you apply only valid filters. It improves security and data integrity.
  • filter_var() and filter_input() help apply these filters dynamically to validate and sanitize user input.
  • filter_id() retrieves the filter ID for a given filter name. It makes it useful for dynamic filtering.

FAQ’s

What is filter_list() in PHP?

filter_list() is a built-in PHP function that returns an array of all supported filters in the filter extension. These filters are used for sanitizing and validating input data.

How does filter_list() work?

filter_list() does not take any parameters. When called, it returns an array of filter names that are available in PHP. You can then use these filters with functions like filter_var() or filter_input(). For example:
$filters = filter_list();
print_r($filters);

How to use filter_list() for input validation?

You can use filter_list() to check available filters and apply them dynamically with filter_var() or filter_input(). Here is an example:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);
    $age = filter_input(INPUT_POST, "age", FILTER_VALIDATE_INT);

    echo ($email && $age !== false) ? "Valid email and age." : "Invalid input.";
}

Similar Reads

Master PHP Iterables: Arrays, Objects, Traversable Interface

The first appearance of PHP iterable was in PHP version ( 7.1 ) – Iterables are a powerful feature in…

PHP array_intersect Function: How it Works with Examples

The PHP array_intersect function finds common values in arrays and returns matches as a new array. Understand the array_intersect in…

PHP Null: How to Assign and Check for Null Values

PHP introduced null to handle undefined or missing values. It helps prevent errors when you check if a variable exists.…

PHP continue Statement: How to Skip Steps in Iteration

The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle…

PHP array_all Function: How it Works with Examples

PHP 8.4 released a new built-in function, array_all checks to check if all array values meet a condition. It gives…

PHP foreach Loop: How to Access Keys & Values in Arrays

PHP foreach loop came to help work with array data in a simple way. Arrays and objects grew more common…

PHP $_FILES: How to Upload Files in PHP

The PHP superglobal $_FILES is a very important utility and great for dealing with file uploads on the web. When a user…

PHP MySQL CRUD: Understanding Database Operations

PHP and MySQL have become an inseparable pair for web developers. One handles the logic, while the other stores the…

PHP array_intersect_uassoc: How it Works with Examples

PHP array_intersect_uassoc checks two or more arrays and returns matches. It compares both the values and the keys with a…

Concatenating Strings in PHP: Tips and Examples

In PHP, string operators, such as the concatenation operator (.) and its assignment variant (.=), are employed for manipulating and…

Previous Article

Git Pull: How to Keep Your Code in Sync

Next Article

PHP filter_var_array: How to Validate Multiple Inputs

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.