|
1039 | 1039 | (<type>int</type>)
|
1040 | 1040 | </term>
|
1041 | 1041 | <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 --> |
1044 | 1042 | <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. |
1046 | 1047 | </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> |
1047 | 1111 | </listitem>
|
1048 | 1112 | </varlistentry>
|
1049 | 1113 | </variablelist>
|
|
0 commit comments