Correction: my last comment was false the function returns 0 instead of FALSE. If an admin could delete both of these notes that would be great.
Filtres de validation
| ID | Nom | Options | Flags | Description |
|---|---|---|---|---|
| FILTER_VALIDATE_BOOLEAN | "boolean" | FILTER_NULL_ON_FAILURE |
Retourne TRUE pour "1", "true", "on" et "yes". Retourne FALSE sinon. Si FILTER_NULL_ON_FAILURE est active, FALSE n'est retourné que pour les valeurs "0", "false", "off", "no", "", et NULL est retourné pour les valeurs non-booléennes. |
|
| FILTER_VALIDATE_EMAIL | "validate_email" | Valide une adresse courriel. | ||
| FILTER_VALIDATE_FLOAT | "float" | decimal | FILTER_FLAG_ALLOW_THOUSAND | Valide un nombre décimal. |
| FILTER_VALIDATE_INT | "int" | min_range, max_range | FILTER_FLAG_ALLOW_OCTAL, FILTER_FLAG_ALLOW_HEX | Valide un entier, éventuellement dans un intervalle donné. |
| FILTER_VALIDATE_IP | "validate_ip" | FILTER_FLAG_IPV4, FILTER_FLAG_IPV6, FILTER_FLAG_NO_PRIV_RANGE, FILTER_FLAG_NO_RES_RANGE | Valide une adresse IP, éventuellement IPv4 ou IPv6, éventuellement hors des plages privées ou réservées. | |
| FILTER_VALIDATE_REGEXP | "validate_regexp" | regexp | Valide une valeur avec une expression rationnelle regexp, compatible Perl. | |
| FILTER_VALIDATE_URL | "validate_url" | FILTER_FLAG_PATH_REQUIRED, FILTER_FLAG_QUERY_REQUIRED | Valide une URL (selon » http://www.faqs.org/rfcs/rfc2396), éventuellement avec des composants requis. Cette fonction ne trouvera que des URLs ASCII valides, les domaines internationnalisés (contenant des caractères non-ASCII) ne fonctionneront pas. |
Note:
Les nombres +0 et -0 ne sont pas des entiers valides, mais plutôt des nombres à virgule flottante valides.
Matthias Dailey
13-Feb-2012 08:51
Matthias Dailey
13-Feb-2012 08:22
FILTER_VALIDATE_INT has a default min_range of 1. This makes it useful for validating HTML select options, where a value of 0 means that no option was selected
Levi Morrison
26-Oct-2011 12:10
It's important to note that in PHP, false==null is true. This means when you are using the FILTER_VALIDATE_BOOLEAN, you must use '===' and '!==' to check to see if something is/isn't null.
<?php
$false = filter_var('0', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
if ($false==null) {
//will execute
}
if ($false===null) {
//will not execute
}
?>
Griff
01-Sep-2011 04:28
<< FILTER_VALIDATE_EMAIL allows incomplete e-mail addresses to be validated, for examle john@gmail will validate as a proper e-mail address >>
"Plain" hostnames with no dots are valid in email addresses -
for example, "me@localhost".
php at sethsyberg dot com
07-Apr-2011 01:00
When validating floats, you must use the Identical/Not identical operators for proper validation of zeros:
This will not work as expected:
<?php
$x = 0;
if (!filter_var($x, FILTER_VALIDATE_FLOAT)) {
echo "$x is a valid float";
} else {
echo "$x is NOT a valid float";
}
?>
This will work as expected:
<?php
$x = 0;
if (filter_var($x, FILTER_VALIDATE_FLOAT)!== false) {
echo "$x is a valid float";
} else {
echo "$x is NOT a valid float";
}
?>
chastell at chastell dot net
15-Mar-2011 07:54
example@example is a perfectly valid email address – I use chastell@localhost and chastell@devielle (my computer’s name) email addresses all the time and they get delivered just fine.
eleljrk at gmail dot com
19-Feb-2011 07:23
For PHP 5.3.1 FILTER_VALIDATE_EMAIL does validate incomplete email addresses such as: example@example
Otherwise it's really good because FILTER_VALIDATE_EMAIL validates the standards of the local part very well.
This is a valid email address:
"this is a valid email@[]{}and it should be"@example.com
And FILTER_VALIDATE_EMAIL validate it.
But this isn't a valid email address:
"this is a valid email@[]{}and it should be"@example
However, FILTER_VALIDATE_EMAIL does validate it.
php dot net at piskvor dot org
11-Feb-2011 08:57
FILTER_VALIDATE_EMAIL is discarding valid e-mail addresses containing IDN. Since there are real, live IDNs on the Internet, that means the filtered output is too strict, leading to false negatives.
Punycode-encoded IDN addresses pass the filter correctly; so before checking for validity, it is necessary to convert the e-mail address to punycode.
Clifton
05-Jan-2011 08:00
FILTER_VALIDATE_EMAIL does NOT allow incomplete e-mail addresses to be validated as mentioned by Tomas.
Using the following code:
<?php
$email = "clifton@example"; //Note the .com missing
echo "PHP Version: ".phpversion().'<br>';
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
echo $email.'<br>';
var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}else{
var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>
Returns:
PHP Version: 5.2.14 //On MY server, may be different depending on which version you have installed.
bool(false)
While the following code:
<?php
$email = "clifton@example.com"; //Note the .com added
echo "PHP Version: ".phpversion().'<br>';
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
echo $email.'<br>';
var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}else{
var_dump(filter_var($email, FILTER_VALIDATE_EMAIL));
}
?>
Returns:
PHP Version: 5.2.14 //On MY server, may be different depending on which version you have installed.
clifton@example.com
string(16) "clifton@example.com"
This feature is only available for PHP Versions (PHP 5 >= 5.2.0) according to documentation. So make sure your version is correct.
Cheers,
Clifton
tomas dot chlouba at gmail dot com
18-Dec-2010 08:42
FILTER_VALIDATE_EMAIL allows incomplete e-mail addresses to be validated, for examle john@gmail will validate as a proper e-mail address.
pravila at alumni dot calpoly dot edu
21-Mar-2010 08:53
Take caution when using the FILTER_VALIDATE_BOOLEAN filter as it seems to have different behaviors when used in the filter_var() vs. the filter_input() functions.
To demonstrate, let's parse some arguments from a GET request (notice how arg2 is NOT set):
example.com/script.php?arg1=yes&arg3=no
<?php
// filtering by variable
$var1 = filter_var($_GET["arg1"], FILTER_VALIDATE_BOOLEAN);
$var2 = filter_var($_GET["arg2"], FILTER_VALIDATE_BOOLEAN);
$var3 = filter_var($_GET["arg3"], FILTER_VALIDATE_BOOLEAN);
// filtering by input
$input1 = filter_input(INPUT_GET, "arg1", FILTER_VALIDATE_BOOLEAN);
$input2 = filter_input(INPUT_GET, "arg2", FILTER_VALIDATE_BOOLEAN);
$input3 = filter_input(INPUT_GET, "arg3", FILTER_VALIDATE_BOOLEAN);
// as expected...
var_dump($var1); // bool(true)
var_dump($var2); // bool(false)
var_dump($var3); // bool(false)
// NULL is not an expected return...
var_dump($input1); // bool(true)
var_dump($input2); // NULL
var_dump($input3); // bool(false)
?>
As per the documentation, the return is limited to true XOR false unless the FILTER_NULL_ON_FAILURE flag is set, but it seems as if this flag is set automatically with the filter_input() function.
(Note: same behavior for filter_var_array() vs. filter_input_array())
