International PHP Conference 2015

Otros filtros

Lista de filtros varios
ID Nombre Opciones Banderas Descripción
FILTER_CALLBACK "callback" Función o método callable   Llamadas a funciones definidas por el usuario para filtrar datos.

add a note add a note

User Contributed Notes 2 notes

up
17
thatindividual dot zhang at gmail dot com
11 months ago
Here is an example, since I cannot find one through out php.net"

<?php

/**
* Strip whitespace (or other non-printable characters) from the beginning and end of a string
* @param string $value
*/
function trimString($value)
{
    return
trim($value);
}

$loginname = filter_input(INPUT_POST, 'loginname', FILTER_CALLBACK, array('options' => 'trimString'));
up
0
christof at himalayasystemsNOSPAM dot be
22 days ago
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
/**
* @property-read $language
*/
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';
        }
    }
}
?>
To Top