PHP Filter Functions

PHP Filter Introduction

PHP Filters are used to validate and filter data from untrusted sources (such as user input).

Installation

Filter functions are an integral part of PHP's core. These functions can be used without installation.

PHP Filter Functions

PHP:Indicates the earliest PHP version that supports this function.

Function Description PHP
filter_has_var() Check if a variable of the specified input type exists. 5
filter_id() Return the ID number of the specified filter. 5
filter_input() Retrieve input from outside the script and filter it. 5
filter_input_array() Retrieve multiple inputs from outside the script and filter them. 5
filter_list() Return an array containing all supported filters. 5
filter_var_array() Retrieve multiple variables and filter them. 5
filter_var() Retrieve a variable and filter it. 5

PHP Filters

ID Name Description
FILTER_CALLBACK Call a user-defined function to filter data.
FILTER_SANITIZE_STRING Remove tags, removing or encoding special characters.
FILTER_SANITIZE_STRIPPED Alias for the "string" filter.
FILTER_SANITIZE_ENCODED URL-encode the string, removing or encoding special characters.
FILTER_SANITIZE_SPECIAL_CHARS HTML escape characters '"<>& and characters with ASCII values less than 32.
FILTER_SANITIZE_EMAIL Remove all characters except letters, numbers, and special characters !#$%&'*+-/=?^_`{|}~@.
FILTER_SANITIZE_URL Remove all characters except letters, numbers, and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=
FILTER_SANITIZE_NUMBER_INT Remove all characters except numbers and +-.
FILTER_SANITIZE_NUMBER_FLOAT Remove all characters except numbers, +-, and .,eE.
FILTER_SANITIZE_MAGIC_QUOTES Apply addslashes().
FILTER_UNSAFE_RAW Do not perform any filtering, remove or encode special characters.
FILTER_VALIDATE_INT Validate the value as an integer within the specified range.
FILTER_VALIDATE_BOOLEAN Returns true if '1', 'true', 'on', 'yes', and if '0', 'false', 'off', 'no', or empty string, returns false. Otherwise, returns NULL.
FILTER_VALIDATE_FLOAT Validate the value as a floating-point number.
FILTER_VALIDATE_REGEXP Validate the value with a regexp, compatible with Perl regular expressions.
FILTER_VALIDATE_URL Validate the value as a URL.
FILTER_VALIDATE_EMAIL Validate the value as an e-mail.
FILTER_VALIDATE_IP Validate the value as an IP address.