Use FILTER_CALLBACK filter to call a user defined function to filter the value in PHP

Description

The following code shows how to use FILTER_CALLBACK filter to call a user defined function to filter the value.

Example


//from   w ww  .  ja  v  a 2  s  .  c  om
<?php
    function convertSpace($string){
        return str_replace(" ", "_", $string);
    }

    $string = "Peter is a great guy!";

    echo filter_var($string, FILTER_CALLBACK,array("options"=>"convertSpace"));
    
    //Using an existing PHP function
    echo filter_var($string, FILTER_CALLBACK, array("options"=>"strtoupper"));
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Data Types »




Array
Array Associative
Array Util
ArrayObject
Data Types
Date
Date Format
DateTime
Number
String
String Escape
String Filter
String HTML
String Type
Timezone