filter_var

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

filter_varFilters a variable with a specified filter

Descrição

filter_var(mixed$value, int$filter = FILTER_DEFAULT, array|int$options = 0): mixed

Parâmetros

value

Value to filter. Note that scalar values are converted to string internally before they are filtered.

filter

O ID do filtro a ser aplicado. A página do manual Tipos de filtros lista os filtros disponíveis.

Se omitido, FILTER_DEFAULT será usado, que é equivalente a FILTER_UNSAFE_RAW. Isso resultará em nenhuma filtragem ocorrendo por padrão.

options

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, callable type should be passed. The callback must accept one argument, the value to be filtered, and return the value after filtering/sanitizing it.

<?php
// for filters that accept options, use this format
$options = array(
'options' => array(
'default' => 3, // value to return if the filter fails

Valor Retornado

Returns the filtered data, or false if the filter fails.

Exemplos

Exemplo #1 A filter_var() example

<?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));
?>

O exemplo acima produzirá:

string(15) "bob@example.com" bool(false)

Exemplo #2 Filter an array example

<?php
$emails
= [
"bob@example.com",
"test@example.local",
"invalidemail"
];

var_dump(filter_var($emails, FILTER_VALIDATE_EMAIL, FILTER_REQUIRE_ARRAY));
?>

O exemplo acima produzirá:

array(3) { [0]=> string(15) "bob@example.com" [1]=> string(18) "test@example.local" [2]=> bool(false) }

Veja Também

To Top