<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- Generated by xml_proto.php v2.4. Found in /scripts directory of phpdoc. -->
<refentry xml:id="function.filter-var" xmlns="http://docbook.org/ns/docbook">
 <refnamediv>
  <refname>filter_var</refname>
  <refpurpose>Filters a variable with a specified filter</refpurpose>
 </refnamediv>
 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>mixed</type><methodname>filter_var</methodname>
   <methodparam><type>mixed</type><parameter>value</parameter></methodparam>
   <methodparam choice="opt"><type>int</type><parameter>filter</parameter><initializer><constant>FILTER_DEFAULT</constant></initializer></methodparam>
   <methodparam choice="opt"><type class="union"><type>array</type><type>int</type></type><parameter>options</parameter><initializer>0</initializer></methodparam>
  </methodsynopsis>

 </refsect1>
 <refsect1 role="parameters">
  &reftitle.parameters;
  <para>
   <variablelist>
    <varlistentry>
     <term><parameter>value</parameter></term>
     <listitem>
      <para>
       Value to filter. Note that scalar values are <link
       linkend="language.types.string.casting">converted to
       string</link> internally before they are filtered.
      </para>
     </listitem>
    </varlistentry>
    &filter.param.filter;
    <varlistentry>
     <term><parameter>options</parameter></term>
     <listitem>
      <para>
       Associative array of options or bitwise disjunction of flags. If filter
       accepts options, flags can be provided in "flags" field of array. For
       the "callback" filter, <type>callable</type> type should be passed.  The
       callback must accept one argument, the value to be filtered, and return
       the value after filtering/sanitizing it.
      </para>
      <para>
       <programlisting role="php">
        <![CDATA[
<?php
// for filters that accept options, use this format
$options = array(
    'options' => array(
        'default' => 3, // value to return if the filter fails
        // other options here
        'min_range' => 0
    ),
    'flags' => FILTER_FLAG_ALLOW_OCTAL,
);
$var = filter_var('0755', FILTER_VALIDATE_INT, $options);

// for filters that only accept flags, you can pass them directly
$var = filter_var('oops', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);

// for filters that only accept flags, you can also pass as an array
$var = filter_var('oops', FILTER_VALIDATE_BOOLEAN,
                  array('flags' => FILTER_NULL_ON_FAILURE));

// callback validate filter
function foo($value)
{
    // Expected format: Surname, GivenNames
    if (strpos($value, ", ") === false) return false;
    list($surname, $givennames) = explode(", ", $value, 2);
    $empty = (empty($surname) || empty($givennames));
    $notstrings = (!is_string($surname) || !is_string($givennames));
    if ($empty || $notstrings) {
        return false;
    } else {
        return $value;
    }
}
$var = filter_var('Doe, Jane Sue', FILTER_CALLBACK, array('options' => 'foo'));
?>
        ]]>
       </programlisting>
      </para>
     </listitem>
    </varlistentry>
   </variablelist>
  </para>
 </refsect1>
 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   Returns the filtered data, or &false; if the filter fails.
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <para>
   <example>
    <title>A <function>filter_var</function> example</title>
    <programlisting role="php">
<![CDATA[
<?php
var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));
var_dump(filter_var('http://example.com', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
string(15) "bob@example.com"
bool(false)
]]>
    </screen>
   </example>
  </para>
  <para>
   <example>
    <title>Filter an array example</title>
    <programlisting role="php">
<![CDATA[
<?php
$emails = [
    "bob@example.com",
    "test@example.local",
    "invalidemail"
];

var_dump(filter_var($emails, FILTER_VALIDATE_EMAIL, FILTER_REQUIRE_ARRAY));
?>
]]>
    </programlisting>
    &example.outputs;
    <screen>
<![CDATA[
array(3) {
  [0]=>
  string(15) "bob@example.com"
  [1]=>
  string(18) "test@example.local"
  [2]=>
  bool(false)
}
]]>
    </screen>
   </example>
  </para>
 </refsect1>

 <refsect1 role="seealso">
  &reftitle.seealso;
  <para>
   <simplelist>
    <member><function>filter_var_array</function></member>
    <member><function>filter_input</function></member>
    <member><function>filter_input_array</function></member>
    <member><xref linkend="filter.filters"/></member>
   </simplelist>
  </para>
 </refsect1>

</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
