PHP Filters Advanced

Sanitizing and validating user input is one of the most common tasks in a web application. To make this task easier PHP provides native filter extension that you can use to sanitize or validate data such as e-mail addresses, URLs, IP addresses, etc.

This function takes three parameters out of which the last two are optional. The first parameter is the value to be filtered, the second parameter is the ID of the filter to apply, and the third parameter is the array of options related to filter. Let's see how it works.

Sanitize a String

Example -

Validate Integer Values

The following example will validate whether the value is a valid integer or not.

Example -

Validate IP Addresses

The following example will validate whether the value is a valid IP address or not.

Example -

Sanitize and Validate Email Addresses

The following example will show you how to sanitize and validate an e-mail address.

Example -

Sanitize and Validate URLs

The following example will show you how to sanitize and validate a url.

Example -

Validate Integers Within a Range

The following example will validate whether the supplied value is an integer or not, as well as whether it lies within the range of 0 to 100 or not.

Example -