Just a little filter to validate IP v4 & v6
This little script display result in function of the query
<?php
$ipv6="2a01:e35:aaa4:6860:a5e7:5ba9:965e:cc93";
$ipv4="82.237.3.3";
$fake = "3342423423";
$ipv4priv = "255.255.255.255";
$ipv6priv = "::1";
echo "<pre>";
echo $ipv4;
echo "<br />";
var_dump(filter_var($ipv4,FILTER_VALIDATE_IP));
echo "<br />";
echo $ipv6;
echo "<br />";
var_dump(filter_var($ipv6,FILTER_VALIDATE_IP));
echo "<br />";
echo $fake;
echo "<br />";
var_dump(filter_var($fake,FILTER_VALIDATE_IP));
echo "<br />";
echo $ipv4priv;
echo "<br/>";
echo "FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE";
echo "<br />";
var_dump(filter_var($ipv4priv,FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE));
echo "<br />";
echo $ipv4priv;
echo "<br/>";
echo "FILTER_FLAG_NO_PRIV_RANGE";
echo "<br />";
var_dump(filter_var($ipv4priv,FILTER_FLAG_NO_PRIV_RANGE));
echo "<br />";
echo $ipv6priv;
echo "<br/>";
echo "FILTER_FLAG_NO_PRIV_RANGE";
echo "<br />";
var_dump(filter_var($ipv6priv,FILTER_FLAG_NO_PRIV_RANGE));
echo "<br />";
echo $ipv6priv;
echo "<br />";
var_dump(filter_var($ipv6priv,FILTER_VALIDATE_IP));
echo "</pre>";
?>
filter_var
(PHP 5 >= 5.2.0)
filter_var — Filtra una variable con un filtro específico
Lista de parámetros
- variable
-
Valor a filtrar, las matrices son filtradas recursivamente.
- filtro
-
ID de un filtro a usar. Su valor predeterminado es FILTER_SANITIZE_STRING.
- opciones
-
Una matriz asociativa de opciones o una disyunción a nivel de bits de banderas. Si el filtro acepta opciones, las banderas pueden definirse en el campo "flags" de la matriz. Para el filtro "callback", debe pasarse un valor tipo callback.
Valores retornados
Devuelve los datos filtrados, o FALSE si el filtro falla.
Ejemplos
Example #1 Un ejemplo de filter_var()
<?php
var_dump(filter_var('roberto@example.com', FILTER_VALIDATE_EMAIL));
var_dump(filter_var('example.com', FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED));
?>
El resultado del ejemplo seria:
string(15) "roberto@example.com" bool(false)
Ver también
- filter_var_array() - Obtiene múltiples variables y opcionalmente las filtra
- filter_input() - Obtiene una variable desde afuera de PHP y opcionalmente la filtra
- filter_input_array() - Obtiene múltiples variables desde afuera de PHP y opcionalmente las filtra
- information about the callback type
filter_var
Meska
30-Apr-2009 07:08
30-Apr-2009 07:08
jon dot bertsch at ucop dot edu
24-Mar-2009 03:49
24-Mar-2009 03:49
Here's an actual example of the filter syntax with a flag since there doesn't appear to be a one liner for this anywhere:
'hours' => array('filter'=>FILTER_SANITIZE_NUMBER_FLOAT, 'flags' => FILTER_FLAG_ALLOW_FRACTION, 'options'=> '.')
dyer85 at gmail dot com
03-Nov-2008 10:00
03-Nov-2008 10:00
Note that when using FILTER_VALIDATE_INT along with the FILTER_FLAG_ALLOW_HEX flag, the string "2f", for example, is not validated successfully, because you must use the "0x" prefix, otherwise, it treats the data as base 10.
The range options are also smart enough to recognize when the boundaries are exceeded in different bases.
Here's an example:
<?php
$foo = '256';
$bar = '0x100';
var_dump(validate_int($foo)); // false, too large
var_dump(validate_int($bar)); // false, too large
function validate_int($input)
{
return filter_var(
$input,
FILTER_VALIDATE_INT,
// We must pass an associative array
// to include the range check options.
array(
'flags' => FILTER_FLAG_ALLOW_HEX,
'options' => array('min_range' => 1, 'max_range' => 0xff)
)
);
}
?>
visseraj at gmail dot com
28-Aug-2008 05:31
28-Aug-2008 05:31
Here are the other possible flags that you can use:
http://us3.php.net/manual/hu/ref.filter.php
dale dot liszka at gmail dot com
09-Jul-2008 05:15
09-Jul-2008 05:15
Here is how to use multiple flags (for those who learn better by example, like me):
<?php
echo "|asdf".chr(9).chr(128)."_123|";
echo "\n";
// "bitwise conjunction" means logic OR / bitwise |
echo filter_var("|asdf".chr(9).chr(128)."_123\n|" ,FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH);
/*
Results:
|asdf �_123|
|asdf_123|
*/
?>
dale dot liszka at gmail dot com
09-Jul-2008 04:54
09-Jul-2008 04:54
Using the FILTER_CALLBACK requires an array to be passed as the options:
<?php
function toDash($x){
return str_replace("_","-",$x);
}
echo filter_var("asdf_123",FILTER_CALLBACK,array("options"=>"toDash"));
// returns 'asdf-123'
?>
John
26-Jul-2007 07:35
26-Jul-2007 07:35
I managed to get this to work with PHP 5.1.6 on CentOS 5 with minor difficulty.
1) Download the PECL filter package
2) Extract the tarball
3) phpize the directory
4) ./configure
5) make
6) filter-0.11.0/logical_filters.c:25:31: error: ext/pcre/php_pcre.h: No such file or directory
7) find / -name php_pcre.h
8) Make sure php-devel is installed
9) Edit filter-0.11.0/logical_filters.c and replace "ext/pcre/php_pcre.h" with the absolute path of php_pcre.h
10) make
11) make install
12) add "extension=filter.so" to php.ini
13) Restart Apache
