©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
ID | Name | Options | Flags | Description |
---|---|---|---|---|
FILTER_CALLBACK | "callback" | callable function or method | Call user-defined function to filter data. |
[#1] christof at himalayasystemsNOSPAM dot be [2015-08-11 10:47:38]
The supplied callback function may also be a class method. (since 5.4.0 ?)
To use a method you need to set the options param to an array with two values: the first is the object and the second is the method name.
<?php
class GetInputStore {
private $allowed_languages = array('en', 'fr', 'de', 'nl' );
private $language;
public function __construct(){
$this->language = filter_input(INPUT_GET, 'language', FILTER_CALLBACK, array('options' => array($this, 'get_language_code')));
}
public function __get($name){
switch($name){
case 'language' : return $this->language;
default : throw new Exception("The GetInputStore class doesn't support GET param \"$name\"");
}
}
private function get_language_code($code){
if(in_array($code, $this->allowed_languages)){
return $code;
} else {
return 'en';
}
}
}
?>
[#2] thatindividual dot zhang at gmail dot com [2014-09-23 07:05:56]
Here is an example, since I cannot find one through out php.net"
<?php
function trimString($value)
{
return trim($value);
}
$loginname = filter_input(INPUT_POST, 'loginname', FILTER_CALLBACK, array('options' => 'trimString'));