PHP Filter Functions
PHP Filter Introduction
PHP filters are used to validate and filter data coming from unsafe sources (such as user input).
Installation
Filter functions are part of the PHP 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() | Checks whether a variable of the specified input type exists. | 5 |
| filter_id() | Returns the ID number of the specified filter. | 5 |
| filter_input() | Gets input from outside the script and filters it. | 5 |
| filter_input_array() | Gets multiple inputs from outside the script and filters them. | 5 |
| filter_list() | Returns an array containing all the supported filters. | 5 |
| filter_var_array() | Gets multiple variables and filters them. | 5 |
| filter_var() | Gets a variable and filters it. | 5 |
PHP Filters
| ID name | Description |
|---|---|
| FILTER_CALLBACK | Calls a user-defined function to filter data. |
| FILTER_SANITIZE_STRING | Strips tags, and removes or encodes special characters. |
| FILTER_SANITIZE_STRIPPED | Alias of the "string" filter. |
| FILTER_SANITIZE_ENCODED | URL-encodes a string, removing or encoding special characters. |
| FILTER_SANITIZE_SPECIAL_CHARS | HTML-escapes the characters ' and \"<>& and characters whose ASCII value is less than 32. |
| FILTER_SANITIZE_EMAIL | Removes all characters except letters, digits, and !#$%&'*+-/=?^_`{|}~@.[] |
| FILTER_SANITIZE_URL | Removes all characters except letters, digits, and $-_.+!*'(),{}|^~[]`<>#%";/?:@&= |
| FILTER_SANITIZE_NUMBER_INT | Removes all characters except digits and +- |
| FILTER_SANITIZE_NUMBER_FLOAT | Removes all characters except digits, +- and .,eE |
| FILTER_SANITIZE_MAGIC_QUOTES | Applies addslashes(). |
| FILTER_UNSAFE_RAW | Does not perform any filtering; removes or encodes special characters. |
| FILTER_VALIDATE_INT | Validates the value as an integer. |
| FILTER_VALIDATE_BOOLEAN | Validates the value as a boolean option. Returns TRUE if it is "1", "true", "on" or "yes". Returns FALSE if it is "0", "false", "off", "no" or "" (empty). Otherwise returns NULL. |
| FILTER_VALIDATE_FLOAT | Validates the value as a float. |
| FILTER_VALIDATE_REGEXP | Validates the value against a regexp (a Perl-compatible regular expression). |
| FILTER_VALIDATE_URL | Validates the value as a URL. |
| FILTER_VALIDATE_EMAIL | Validates the value as an e-mail address. |
| FILTER_VALIDATE_IP | Validates the value as an IP address, limited to IPv4 or IPv6, or not from a private or reserved range. |