Home  >  Article  >  Backend Development  >  Analysis of filter_var() function and Filter function in PHP

Analysis of filter_var() function and Filter function in PHP

不言
不言Original
2018-06-21 13:54:011339browse

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(&#39;content-type:text/html;charset=utf-8;&#39;); 
$email_a=&#39;jcifox@gmail.com&#39;; 
$email_b=&#39;@jcifox@gmail.com&#39;; 
$email_c=&#39;jcifoxgmail.com&#39;; 
$ip_a=&#39;0.0.0.0&#39;; 
$ip_b=&#39;255.255.255.255&#39;; 
$ip_c=&#39;0.0.0.265&#39;; 
echo $email_a.&#39; : &#39;; 
echo (filter_var($email_a,FILTER_VALIDATE_EMAIL))?&#39;is valid&#39;:&#39;is not valid&#39;; 
echo &#39;<br /><br />&#39;; 
echo $email_b.&#39; : &#39;; 
echo (filter_var($email_b,FILTER_VALIDATE_EMAIL))?&#39;is valid&#39;:&#39;is not valid&#39;; 
echo &#39;<br /><br />&#39;; 
echo $email_c.&#39; : &#39;; 
echo (filter_var($email_c,FILTER_VALIDATE_EMAIL))?&#39;is valid&#39;:&#39;is not valid&#39;; 
echo &#39;<br /><br />&#39;; 
echo $ip_a.&#39; : &#39;; 
echo (filter_var($ip_a,FILTER_VALIDATE_IP))?&#39;is valid&#39;:&#39;is not valid&#39;; 
echo &#39;<br /><br />&#39;; 
echo $ip_b.&#39; : &#39;; 
echo (filter_var($ip_b,FILTER_VALIDATE_IP))?&#39;is valid&#39;:&#39;is not valid&#39;; 
echo &#39;<br /><br />&#39;; 
echo $ip_c.&#39; : &#39;; 
echo (filter_var($ip_c,FILTER_VALIDATE_IP))?&#39;is valid&#39;:&#39;is not valid&#39;; 
?>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn