<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filter.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'pt_BR',
  ),
  'this' => 
  array (
    0 => 'function.filter-var.php',
    1 => 'filter_var',
    2 => 'Filtra uma vari&aacute;vel com um filtro especificado',
  ),
  'up' => 
  array (
    0 => 'ref.filter.php',
    1 => 'Fun&ccedil;&otilde;es da Filter',
  ),
  'prev' => 
  array (
    0 => 'function.filter-list.php',
    1 => 'filter_list',
  ),
  'next' => 
  array (
    0 => 'function.filter-var-array.php',
    1 => 'filter_var_array',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'pt_BR',
    'path' => 'reference/filter/functions/filter-var.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.filter-var" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">filter_var</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">filter_var</span> &mdash; <span class="dc-title">Filtra uma variável com um filtro especificado</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.filter-var-description">
  <h3 class="title">Descrição</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>filter_var</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$filter</code><span class="initializer"> = <strong><code><a href="filter.constants.php#constant.filter-default">FILTER_DEFAULT</a></code></strong></span></span>, <span class="methodparam"><span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.integer.php" class="type int">int</a></span></span> <code class="parameter">$options</code><span class="initializer"> = 0</span></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>

  <p class="simpara">
   Filtra uma variável usando filtros de validação
   <strong><code><a href="filter.constants.php#constant.filter-validate-bool">FILTER_VALIDATE_<span class="replaceable">*</span></a></code></strong>,
   filtros de sanitização
   <strong><code><a href="filter.constants.php#constant.filter-sanitize-string">FILTER_SANITIZE_<span class="replaceable">*</span></a></code></strong>,
   ou um filtro customizado.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.filter-var-parameters">
  <h3 class="title">Parâmetros</h3>
  <dl>
   
    <dt><code class="parameter">value</code></dt>
    <dd>
     <span class="simpara">
      Valor a filtrar.
     </span>
     <div class="warning"><strong class="warning">Aviso</strong>
      <p class="simpara">
       Valores escalares são
       <a href="language.types.string.php#language.types.string.casting" class="link">convertidos para strings</a>
       internamente antes de serem filtrados.
      </p>
     </div>
    </dd>
   
   
    <dt><code class="parameter">filter</code></dt>
    <dd>
     <span class="simpara">
      O filtro a ser aplicado.
      Pode ser um filtro de validação pelo uso de uma das constantes
      <strong><code><a href="filter.constants.php#constant.filter-validate-bool">FILTER_VALIDATE_<span class="replaceable">*</span></a></code></strong>,
      um filtro de sanitização pelo uso de uma das constantes
      <strong><code><a href="filter.constants.php#constant.filter-sanitize-string">FILTER_SANITIZE_<span class="replaceable">*</span></a></code></strong>
      ou de <strong><code><a href="filter.constants.php#constant.filter-unsafe-raw">FILTER_UNSAFE_RAW</a></code></strong>, ou um filtro cutomizado com o uso de
      <strong><code><a href="filter.constants.php#constant.filter-callback">FILTER_CALLBACK</a></code></strong>.
     </span>
     <blockquote class="note"><p><strong class="note">Nota</strong>: 
      <span class="simpara">
       O padrão é <strong><code><a href="filter.constants.php#constant.filter-default">FILTER_DEFAULT</a></code></strong>,
       que é um sinônimo de <strong><code><a href="filter.constants.php#constant.filter-unsafe-raw">FILTER_UNSAFE_RAW</a></code></strong>.
       Isto resultará em nenhuma filtragem por padrão.
      </span>
     </p></blockquote>
    </dd>
   
   
    <dt><code class="parameter">options</code></dt>
    <dd>
     <span class="simpara">
      Um <span class="type"><a href="language.types.array.php" class="type array">array</a></span> associativo de opções,
      ou uma máscara de bits de constantes de opções de filtro
      <strong><code><a href="filter.constants.php#constant.filter-flag-none">FILTER_FLAG_<span class="replaceable">*</span></a></code></strong>.
     </span>
     <span class="simpara">
      Se <code class="parameter">filter</code> aceitar opções,
      elas podem ser fornecidas pelo uso do campo <code class="literal">&quot;flags&quot;</code> do array.
     </span>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.filter-var-returnvalues">
  <h3 class="title">Valor Retornado</h3>
  <p class="simpara">
   Em caso de sucesso, retorna os dados filtrados.
   Em caso de falha, retorna <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>,
   a não ser que a opção <strong><code><a href="filter.constants.php#constant.filter-null-on-failure">FILTER_NULL_ON_FAILURE</a></code></strong> seja usada,
   neste caso retorna <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.filter-var-examples">
  <h3 class="title">Exemplos</h3>
  <div class="example" id="example-1">
   <p><strong>Exemplo #1 Um exemplo de <span class="function"><strong>filter_var()</strong></span></strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #DD0000">'bob@example.com'</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_VALIDATE_EMAIL</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #DD0000">'https://example.com'</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_VALIDATE_URL</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_FLAG_PATH_REQUIRED</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>O exemplo acima produzirá:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">string(15) &quot;bob@example.com&quot;
bool(false)</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-2">
   <p><strong>Exemplo #2 Exemplo de validação de entradas de um array</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$emails </span><span style="color: #007700">= [<br />    </span><span style="color: #DD0000">"bob@example.com"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"test@example.local"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">"invalidemail"<br /></span><span style="color: #007700">];<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$emails</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_VALIDATE_EMAIL</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_REQUIRE_ARRAY</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>O exemplo acima produzirá:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">array(3) {
  [0]=&gt;
  string(15) &quot;bob@example.com&quot;
  [1]=&gt;
  string(18) &quot;test@example.local&quot;
  [2]=&gt;
  bool(false)
}</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-3">
   <p><strong>Exemplo #3 Exemplo de passagem de um array em <code class="parameter">options</code></strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$options </span><span style="color: #007700">= [<br />    </span><span style="color: #DD0000">'options' </span><span style="color: #007700">=&gt; [<br />        </span><span style="color: #DD0000">'min_range' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">10</span><span style="color: #007700">,<br />    ],<br />    </span><span style="color: #DD0000">'flags' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_FLAG_ALLOW_OCTAL</span><span style="color: #007700">,<br />];<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #DD0000">'0755'</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #DD0000">'011'</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_VALIDATE_INT</span><span style="color: #007700">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>O exemplo acima produzirá:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">int(493)
bool(false)</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-4">
   <p><strong>Exemplo #4 Fornecendo opções diretamente ou através de um <span class="type"><a href="language.types.array.php" class="type array">array</a></span></strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">'string'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_VALIDATE_BOOLEAN</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_NULL_ON_FAILURE</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">filter_var</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #0000BB">FILTER_VALIDATE_BOOLEAN</span><span style="color: #007700">, [</span><span style="color: #DD0000">'flags' </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">FILTER_NULL_ON_FAILURE</span><span style="color: #007700">]));<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>O exemplo acima produzirá:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">NULL
NULL</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.filter-var-seealso">
  <h3 class="title">Veja Também</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.filter-var-array.php" class="function" rel="rdfs-seeAlso">filter_var_array()</a> - Obt&ecirc;m e opcionalmente filtra m&uacute;ltiplas vari&aacute;veis</span></li>
   <li><span class="function"><a href="function.filter-input.php" class="function" rel="rdfs-seeAlso">filter_input()</a> - Obt&eacute;m uma vari&aacute;vel externa espec&iacute;fica por nome e filtra-a opcionalmente</span></li>
   <li><span class="function"><a href="function.filter-input-array.php" class="function" rel="rdfs-seeAlso">filter_input_array()</a> - Obtem vari&aacute;veis externas e opcionalmente as filtra</span></li>
   <li>
    Filtros de validação
    <strong><code><a href="filter.constants.php#constant.filter-validate-bool">FILTER_VALIDATE_<span class="replaceable">*</span></a></code></strong>
   </li>
   <li>
    Filtros de sanitização
    <strong><code><a href="filter.constants.php#constant.filter-sanitize-string">FILTER_SANITIZE_<span class="replaceable">*</span></a></code></strong>
   </li>
  </ul>
 </div>


</div><?php manual_footer($setup); ?>