Константи, описані нижче — визначені цим розширенням, тож доступні, коли розширення скомпільовано як частина PHP або динамічно підключене під час виконання.
FILTER_CALLBACK
(int)
This filter delegates the filtering to a user defined function.
The callable is passed via the
options parameter as the value associated to
the 'options' key.
The callback should have the following signature:
valueThe value that is being filtered.
Зауваження:
The value returned by the callback will be the value returned by the invoked filter function.
Приклад #1
Example of using FILTER_CALLBACK to validate
a login name
<?php
function validate_login(string $value): ?string
{
if (strlen($value) >= 5 && ctype_alnum($value)) {
return $value;
}
return null;
}
$login = "val1dL0gin";
$filtered_login = filter_var($login, FILTER_CALLBACK, ['options' => 'validate_login']);
var_dump($filtered_login);
$login = "f&ke login";
$filtered_login = filter_var($login, FILTER_CALLBACK, ['options' => 'validate_login']);
var_dump($filtered_login);
?>Поданий вище приклад виведе:
string(10) "val1dL0gin" NULL
Зауваження:
As of PHP 8.1.25 and 8.2.12, the structural flags
FILTER_REQUIRE_ARRAY,FILTER_FORCE_ARRAYandFILTER_REQUIRE_SCALARapply to this filter, andFILTER_NULL_ON_FAILUREchanges how such a structural failure is reported. In earlier versions all flags were ignored. Filter specific flags are always ignored, and since the return value of the callback is the result, the callback itself cannot fail: no flag turns afalsereturned by the callback intonullor an exception.