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|
*/
?>
filter_var
(PHP 5 >= 5.2.0, PECL filter:0.11.0)
filter_var — Filtern einer Variablen durch einen spezifischen Filter.
Parameter-Liste
- variable
-
Wert der gefiltert werden soll. Arrays werden rekursiv gefiltert.
- filter
-
ID des zu benutztenden Filters. Standard ist FILTER_SANITIZE_STRING.
- options
-
Assoziatives Array mit Optionen oder bitweise Disjunktion von Flags. Wenn der Filter Optionen akzeptiert, können Flags auch im "flags" Feld des Arrays angegeben werden. Für "callback" Filter sollte der callback-Typ angegeben werden.
Rückgabewerte
Gibt die gefilterten Daten zurück oder FALSE wenn fehlgeschlagen.
Beispiele
Beispiel #1 filter_var() Beispiel
<?php
var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));
var_dump(filter_var('example.com', FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED));
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
string(15) "bob@example.com" bool(false)
Siehe auch
- filter_var_array()
- filter_input()
- filter_input_array()
- Informationen über den Callback-Typ
filter_var
dale dot liszka at gmail dot com
09-Jul-2008 10:15
09-Jul-2008 10:15
dale dot liszka at gmail dot com
09-Jul-2008 09:54
09-Jul-2008 09: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 12:35
26-Jul-2007 12: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
menic [monkey] o2 . pl
25-May-2007 10:46
25-May-2007 10:46
I dont know why, but this function doesn't work properly with array as $variable. To make it work I suggest to use foreach, and filter each value in this way.
