PHP FILTER_CALLBACK Filter
Definition and Usage
The FILTER_CALLBACK filter uses a user-defined function to filter values.
This filter gives us complete control over data filtering.
The specified function must be stored in an associative array named "options". See the example below.
- Name: "callback"
- ID-number: 1024
Tips and Comments
Tips:You can create your own functions or use existing PHP functions.
Examples
Example 1
<?php
function convertSpace($string)
{
return str_replace(" ", "_", $string);
}
$string = "Peter is a great guy!";
echo filter_var($string, FILTER_CALLBACK
,
array("options"=>"convertSpace"));
?>
Output:
Peter_is_a_great_guy!
Example 2
<?php
$string="Peter is a great guy!";
echo filter_var($string, FILTER_CALLBACK
,
array("options"=>"strtoupper"));
?>
Output:
PETER IS A GREAT GUY!