©
Ce document utilise Manuel du site Web PHP chinois Libérer
Example #1 Validating email addresses with filter_var()
<?php
$email_a = 'joe@example.com' ;
$email_b = 'bogus' ;
if ( filter_var ( $email_a , FILTER_VALIDATE_EMAIL )) {
echo "This ( $email_a ) email address is considered valid." ;
}
if ( filter_var ( $email_b , FILTER_VALIDATE_EMAIL )) {
echo "This ( $email_b ) email address is considered valid." ;
}
?>
以上例程会输出:
This (joe@example.com) email address is considered valid.
Example #2 Validating IP addresses with filter_var()
<?php
$ip_a = '127.0.0.1' ;
$ip_b = '42.42' ;
if ( filter_var ( $ip_a , FILTER_VALIDATE_IP )) {
echo "This (ip_a) IP address is considered valid." ;
}
if ( filter_var ( $ip_b , FILTER_VALIDATE_IP )) {
echo "This (ip_b) IP address is considered valid." ;
}
?>
以上例程会输出:
This (ip_a) IP address is considered valid.
Example #3 Passing options to filter_var()
<?php
$int_a = '1' ;
$int_b = '-1' ;
$int_c = '4' ;
$options = array(
'options' => array(
'min_range' => 0 ,
'max_range' => 3 ,
)
);
if ( filter_var ( $int_a , FILTER_VALIDATE_INT , $options ) !== FALSE ) {
echo "This (int_a) integer is considered valid (between 0 and 3).\n" ;
}
if ( filter_var ( $int_b , FILTER_VALIDATE_INT , $options ) !== FALSE ) {
echo "This (int_b) integer is considered valid (between 0 and 3).\n" ;
}
if ( filter_var ( $int_c , FILTER_VALIDATE_INT , $options ) !== FALSE ) {
echo "This (int_c) integer is considered valid (between 0 and 3).\n" ;
}
$options [ 'options' ][ 'default' ] = 1 ;
if (( $int_c = filter_var ( $int_c , FILTER_VALIDATE_INT , $options )) !== FALSE ) {
echo "This (int_c) integer is considered valid (between 0 and 3) and is $int_c ." ;
}
?>
以上例程会输出:
This (int_a) integer is considered valid (between 0 and 3). This (int_c) integer is considered valid (between 0 and 3) and is 1.
[#1] ghazouanijs at gmail dot com [2015-01-20 15:49:51]
invalid email skip this filter
example : gnix@lineone.netsteve.gynes@lane4.co.uk
[#2] makicar5 at gmail dot com [2014-01-20 15:28:51]
Email example is correct, in other examples just add dollar sign:
echo "This (ip_a) IP address is considered valid.";
echo "This (ip_a) IP address is considered valid.";
Change to:
echo "This ($ip_a) IP address is considered valid.";
echo "This ($ip_a) IP address is considered valid.";
same for
int_a, int_b, int_c
to $int_a, $int_b, $int_c
Don't use single quotes in this example!
[#3] brokendocs at php dot net [2013-11-08 13:50:25]
all the above documentation is slightly incorrect, the output is not reading the variables in -
code:
$email_a = 'joe@example.com';
$email_b = 'bogus';
if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) {
echo "This ($email_a) email address is considered valid.";
}
output:
This (email_a) email address is considered valid.
should be output:
This (joe@example.com) email address is considered valid.
same for the IP and integer examples.
[#4] Stan [2013-09-11 10:02:55]
HP 5.3.3 and 5.2.14 had a bug (http://bugs.php.net/52929) related to FILTER_VALIDATE_EMAIL, which resulted in segfault when validating large values. Simple and safe workaround for this is using strlen() before filter_val(). I'm not sure about 5.3.4 final, but it is written that some 5.3.4-snapshot versions also were affected.
Originally posted here - http://stackoverflow.com/questions/5855811/how-to-validate-an-email-in-php#comment6729146_5855853.