Home > Article > Backend Development > Analysis of filter_var() function and Filter function in PHP
This article mainly introduces the analysis of the filter_var() function and Filter function in PHP. It has a certain reference value. Now I share it with you. Friends in need can refer to it
PHP filter is used Validate and filter data from non-secure sources (such as user input)
filter_var() function filters a variable through a specified filter.
If successful, return filtered data, if failed, return false.
Syntax
filter_var(variable, filter, options)variable: required. Specifies the variables to filter.
filter: optional. Specifies the ID of the filter to use. (See FiltersID list below)
options: Specifies an array containing flags/options. Check the possible flags and options for each filter.
<? @header('content-type:text/html;charset=utf-8;'); $email_a='jcifox@gmail.com'; $email_b='@jcifox@gmail.com'; $email_c='jcifoxgmail.com'; $ip_a='0.0.0.0'; $ip_b='255.255.255.255'; $ip_c='0.0.0.265'; echo $email_a.' : '; echo (filter_var($email_a,FILTER_VALIDATE_EMAIL))?'is valid':'is not valid'; echo '<br /><br />'; echo $email_b.' : '; echo (filter_var($email_b,FILTER_VALIDATE_EMAIL))?'is valid':'is not valid'; echo '<br /><br />'; echo $email_c.' : '; echo (filter_var($email_c,FILTER_VALIDATE_EMAIL))?'is valid':'is not valid'; echo '<br /><br />'; echo $ip_a.' : '; echo (filter_var($ip_a,FILTER_VALIDATE_IP))?'is valid':'is not valid'; echo '<br /><br />'; echo $ip_b.' : '; echo (filter_var($ip_b,FILTER_VALIDATE_IP))?'is valid':'is not valid'; echo '<br /><br />'; echo $ip_c.' : '; echo (filter_var($ip_c,FILTER_VALIDATE_IP))?'is valid':'is not valid'; ?>
FiltersID name: Description
FILTER_CALLBACK: Call user-defined function to filter data.
FILTER_SANITIZE_STRING: Remove tags, remove or encode special characters.
FILTER_SANITIZE_STRIPPED: Alias for "string" filter.
FILTER_SANITIZE_ENCODED: URL-encode string, remove or encode special characters.
FILTER_SANITIZE_SPECIAL_CHARS: HTML escape characters '"a8093152e673feb7aba1828c43532094& and characters with ASCII value less than 32.
FILTER_SANITIZE_EMAIL: Remove all characters except letters, numbers and !#$%&'* -/=?^ _`{|}~@.[]
FILTER_SANITIZE_URL: Delete all characters except letters, numbers and $-_. !*'(),{}|\\^~[]`a8093152e673feb7aba1828c43532094#% ";/?:@&=
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: No filtering, removal or encoding of special characters.
FILTER_VALIDATE_INT: Validates values as integers in the specified range.
FILTER_VALIDATE_BOOLEAN: If it is "1", "true", "on" and "yes", it returns true, if it is "0", "false", "off", "no" and "", it returns false. Otherwise NULL is returned.
FILTER_VALIDATE_FLOAT: Validate value as a floating point number.
FILTER_VALIDATE_REGEXP: Validate values based on regexp, a Perl-compatible regular expression.
FILTER_VALIDATE_URL: Validate the value as a URL.
FILTER_VALIDATE_EMAIL: Validate value as e-mail.
FILTER_VALIDATE_IP: Validate the value as an IP address.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
About the analysis of the urlencode() URL encoding function in php
##About php_pdo Pre- Parsing of processing statements
The above is the detailed content of Analysis of filter_var() function and Filter function in PHP. For more information, please follow other related articles on the PHP Chinese website!