<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/filter.constants.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'filter.constants.callback.php',
    1 => 'User Defined Filter',
    2 => 'User Defined Filter',
  ),
  'up' => 
  array (
    0 => 'filter.constants.php',
    1 => 'Predefined Constants',
  ),
  'prev' => 
  array (
    0 => 'filter.constants.sanitization.php',
    1 => 'Sanitizing Filters',
  ),
  'next' => 
  array (
    0 => 'filter.examples.php',
    1 => 'Examples',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/filter/constants.callback.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="filter.constants.callback" class="section">
 <h2 class="title">User Defined Filter</h2>
 <p class="simpara">
The constants below are defined by this extension, and
will only be available when the extension has either
been compiled into PHP or dynamically loaded at runtime.
</p>
 <dl>
  
   <dt id="constant.filter-callback">
    <strong><code><a href="filter.constants.callback.php#constant.filter-callback">FILTER_CALLBACK</a></code></strong>
    (<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>)
   </dt>
   <dd>
    <p class="simpara">
     This filter delegates the filtering to a user defined function.
     The <span class="type"><a href="language.types.callable.php" class="type callable">callable</a></span> is passed via the
     <code class="parameter">options</code> parameter as the value associated to
     the <code class="literal">&#039;options&#039;</code> key.
    </p>
    <p class="para">
     The callback should have the following signature:
     <div class="methodsynopsis dc-description">
      <span class="modifier">function</span> <span class="methodname"><span class="replaceable">callback</span></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$value</code></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

     <dl>
      
       <dt><code class="parameter">value</code></dt>
       <dd>
        <p class="simpara">
         The value that is being filtered.
        </p>
       </dd>
      
     </dl>
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="simpara">
      The value returned by the callback will be the value returned by
      the invoked filter function.
     </p>
    </p></blockquote>
    <div class="example" id="example-1">
     <p><strong>Example #1 
      Example of using <strong><code><a href="filter.constants.callback.php#constant.filter-callback">FILTER_CALLBACK</a></code></strong> to validate
      a login name
     </strong></p>
     <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">function </span><span style="color: #0000BB">validate_login</span><span style="color: #007700">(</span><span style="color: #0000BB">string $value</span><span style="color: #007700">): ?</span><span style="color: #0000BB">string
</span><span style="color: #007700">{
    if (</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">) &gt;= </span><span style="color: #0000BB">5 </span><span style="color: #007700">&amp;&amp; </span><span style="color: #0000BB">ctype_alnum</span><span style="color: #007700">(</span><span style="color: #0000BB">$value</span><span style="color: #007700">)) {
        return </span><span style="color: #0000BB">$value</span><span style="color: #007700">;
    }
    return </span><span style="color: #0000BB">null</span><span style="color: #007700">;
}

</span><span style="color: #0000BB">$login </span><span style="color: #007700">= </span><span style="color: #DD0000">"val1dL0gin"</span><span style="color: #007700">;
</span><span style="color: #0000BB">$filtered_login </span><span style="color: #007700">= </span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$login</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_CALLBACK</span><span style="color: #007700">, [</span><span style="color: #DD0000">'options' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'validate_login'</span><span style="color: #007700">]);
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$filtered_login</span><span style="color: #007700">);

</span><span style="color: #0000BB">$login </span><span style="color: #007700">= </span><span style="color: #DD0000">"f&amp;ke login"</span><span style="color: #007700">;
</span><span style="color: #0000BB">$filtered_login </span><span style="color: #007700">= </span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$login</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_CALLBACK</span><span style="color: #007700">, [</span><span style="color: #DD0000">'options' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">'validate_login'</span><span style="color: #007700">]);
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$filtered_login</span><span style="color: #007700">);
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
     </div>

     <div class="example-contents"><p>The above example will output:</p></div>
     <div class="example-contents screen">
<div class="cdata"><pre>
string(10) &quot;val1dL0gin&quot;
NULL
</pre></div>
     </div>
    </div>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="simpara">
      As of PHP 8.1.25 and 8.2.12, the structural flags
      <strong><code><a href="filter.constants.flags.generic.php#constant.filter-require-array">FILTER_REQUIRE_ARRAY</a></code></strong>,
      <strong><code><a href="filter.constants.flags.generic.php#constant.filter-force-array">FILTER_FORCE_ARRAY</a></code></strong> and
      <strong><code><a href="filter.constants.flags.generic.php#constant.filter-require-scalar">FILTER_REQUIRE_SCALAR</a></code></strong> apply to this filter, and
      <strong><code><a href="filter.constants.flags.generic.php#constant.filter-null-on-failure">FILTER_NULL_ON_FAILURE</a></code></strong> changes 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 a <code class="literal">false</code> returned by the callback into
      <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> or an exception.
     </p>
    </p></blockquote>
   </dd>
  
 </dl>
</div><?php manual_footer($setup); ?>