Skip to content

Commit 2f3041d

Browse files
Filter: Document FILTER_CALLBACK (#4143)
Co-authored-by: Kamil Tekiela <[email protected]>
1 parent 8b502f8 commit 2f3041d

File tree

2 files changed

+67
-34
lines changed

2 files changed

+67
-34
lines changed

reference/filter/constants.xml

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,11 +1039,75 @@
10391039
(<type>int</type>)
10401040
</term>
10411041
<listitem>
1042-
<!-- TODO: Document how to use this filter, see user notes on manual page:
1043-
https://www.php.net/manual/en/filter.filters.misc.php -->
10441042
<simpara>
1045-
ID of "callback" filter.
1043+
This filter delegates the filtering to a user defined function.
1044+
The <type>callable</type> is passed via the
1045+
<parameter>options</parameter> parameter as the value associated to
1046+
the <literal>'options'</literal> key.
10461047
</simpara>
1048+
<para>
1049+
The callback should have the following signature:
1050+
<methodsynopsis>
1051+
<type>mixed</type><methodname><replaceable>callback</replaceable></methodname>
1052+
<methodparam><type>string</type><parameter>value</parameter></methodparam>
1053+
</methodsynopsis>
1054+
<variablelist role="function_parameters">
1055+
<varlistentry>
1056+
<term><parameter>value</parameter></term>
1057+
<listitem>
1058+
<simpara>
1059+
The value that is being filtered.
1060+
</simpara>
1061+
</listitem>
1062+
</varlistentry>
1063+
</variablelist>
1064+
</para>
1065+
<note>
1066+
<simpara>
1067+
The value returned by the callback will be the value returned by
1068+
the invoked filter function.
1069+
</simpara>
1070+
</note>
1071+
<example>
1072+
<title>
1073+
Example of using <constant>FILTER_CALLBACK</constant> to validate
1074+
a login name
1075+
</title>
1076+
<programlisting role="php">
1077+
<![CDATA[
1078+
<?php
1079+
function validate_login($value): ?string
1080+
{
1081+
if (strlen($value) >= 5 && ctype_alnum($value)) {
1082+
return $value;
1083+
}
1084+
return null;
1085+
}
1086+
1087+
$login = "val1dL0gin";
1088+
$filtered_login = filter_var($login, FILTER_CALLBACK, ['options' => 'validate_login']);
1089+
var_dump($filtered_login);
1090+
1091+
$login = "f&ke login";
1092+
$filtered_login = filter_var($login, FILTER_CALLBACK, ['options' => 'validate_login']);
1093+
var_dump($filtered_login);
1094+
?>
1095+
]]>
1096+
</programlisting>
1097+
&example.outputs;
1098+
<screen>
1099+
<![CDATA[
1100+
string(10) "val1dL0gin"
1101+
NULL
1102+
]]>
1103+
</screen>
1104+
</example>
1105+
<warning>
1106+
<simpara>
1107+
This filter cannot be used with any other filter flags, e.g.
1108+
<constant>FILTER_NULL_ON_FAILURE</constant>.
1109+
</simpara>
1110+
</warning>
10471111
</listitem>
10481112
</varlistentry>
10491113
</variablelist>

reference/filter/filters.xml

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -129,37 +129,6 @@
129129
</section>
130130
<!--}}}-->
131131

132-
<!-- Other filters: {{{-->
133-
<section xml:id="filter.filters.misc">
134-
<title>Other filters</title>
135-
<para>
136-
<table>
137-
<title>List of miscellaneous filters</title>
138-
<tgroup cols="5">
139-
<thead>
140-
<row>
141-
<entry>ID</entry>
142-
<entry>Name</entry>
143-
<entry>Options</entry>
144-
<entry>Flags</entry>
145-
<entry>Description</entry>
146-
</row>
147-
</thead>
148-
<tbody>
149-
<row>
150-
<entry><constant>FILTER_CALLBACK</constant></entry>
151-
<entry>"callback"</entry>
152-
<entry><type>callable</type> function or method</entry>
153-
<entry>All flags are ignored</entry>
154-
<entry>Call user-defined function to filter data.</entry>
155-
</row>
156-
</tbody>
157-
</tgroup>
158-
</table>
159-
</para>
160-
</section>
161-
<!--}}}-->
162-
163132
<!-- Filter flags: {{{-->
164133
<section xml:id="filter.filters.flags">
165134
<title>Filter flags</title>

0 commit comments

Comments
 (0)