Latest web development tutorials

PHP FILTER_CALLBACK filter

PHP Filter Reference Complete PHP Filter Reference

Definition and Usage

FILTER_CALLBACK filter calls a user-defined function to filter data.

This filter gives us full control over data filtering.

The specified function must be stored in an associative array named "options" in. See the following examples.

  • Name: "callback"
  • ID-number: 1024

Tips and Notes

Tip: You can create your own functions or use existing PHP functions.


Example 1

Use user-defined functions:

<?php
function convertSpace($string)
{
return str_replace(" ", "_", $string);
}

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

echo filter_var($string, FILTER_CALLBACK,
array("options"=>"convertSpace"));
?>

Output code is as follows:

Peter_is_a_great_guy!


Example 2

Use existing PHP functions:

<?php
$string="Peter is a great guy!";

echo filter_var($string, FILTER_CALLBACK,
array("options"=>"strtoupper"));
?>

Output code is as follows:

PETER IS A GREAT GUY!


PHP Filter Reference Complete PHP Filter Reference