PHP filter_var of function Filter function

  • 2020-05-16 06:32:08
  • OfStack

The filter_var() function filters variables through the specified filter.
If successful, the filtered data is returned, and if unsuccessful, false is returned.
grammar
filter_var(variable, filter, options)variable: required. Specify the variables to filter.
filter: optional. Specify the ID of the filter to be used. (see the FiltersID list below)
options: specifies an array of flags/options. Check each filter for possible flags and options.
 
<? 
@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: calls user-defined functions to filter data.
FILTER_SANITIZE_STRING: removes labels, removes or encodes special characters.
FILTER_SANITIZE_STRIPPED: alias for "string" filter.
FILTER_SANITIZE_ENCODED: URL-encode string that removes or encodes special characters.
FILTER_SANITIZE_SPECIAL_CHARS: HTML escape character '" " < > & And characters with an ASCII value less than 32.
FILTER_SANITIZE_EMAIL: delete all characters except letters, Numbers and! # $% & '*+-/=?^_`{|}~@.[]
FILTER_SANITIZE_URL: delete all characters except letters, Numbers and $-_.+! (*), {} | \ \ ^ ~ [] ` < > #%";/?:@ & =
FILTER_SANITIZE_NUMBER_INT: delete all characters except Numbers and +-
FILTER_SANITIZE_NUMBER_FLOAT: delete all characters except digits, +-, and.,eE.
FILTER_SANITIZE_MAGIC_QUOTES: apply addslashes().
FILTER_UNSAFE_RAW: no filtering, removal or encoding of special characters.
FILTER_VALIDATE_INT: verifies the value with an integer in the specified range.
FILTER_VALIDATE_BOOLEAN: returns true if "1", "true", "on" and "yes", and false if "0", "false", "off", "no" and "yes". Otherwise return NULL.
FILTER_VALIDATE_FLOAT: validates a value with a floating point number.
FILTER_VALIDATE_REGEXP: validate values according to regexp, Perl compatible regular expressions.
FILTER_VALIDATE_URL: verify the value as URL.
FILTER_VALIDATE_EMAIL: verify the value as e-mail.
FILTER_VALIDATE_IP: verify the value as the IP address.

Related articles: