filter_var

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

filter_varFilters a variable with a specified filter

Açıklama

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

Filter a variable using a FILTER_VALIDATE_* validation filters, a FILTER_SANITIZE_* sanitization filters, or a custom filter.

Bağımsız Değişkenler

value
Value to filter.
Uyarı

Scalar values are converted to string internally before they are filtered.

filter
The filter to apply. Can be a validation filter by using one of the FILTER_VALIDATE_* constants, a sanitization filter by using one of the FILTER_SANITIZE_* or FILTER_UNSAFE_RAW, or a custom filter by using FILTER_CALLBACK.

Bilginize: The default is FILTER_DEFAULT, which is an alias of FILTER_UNSAFE_RAW. This will result in no filtering taking place by default.

options
Either an associative array of options, or a bitmask of filter flag constants FILTER_FLAG_*. If the filter accepts options, flags can be provided by using the "flags" field of array.

Dönen Değerler

On success returns the filtered data. On failure false is returned, unless the FILTER_NULL_ON_FAILURE flag is used, in which case null is returned.

Örnekler

Örnek 1 A filter_var() example

<?php
var_dump
(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));
var_dump(filter_var('https://example.com', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED));
?>

Yukarıdaki örneğin çıktısı:

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

Örnek 2 Example validating entries of an array

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

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

Yukarıdaki örneğin çıktısı:

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

Örnek 3 Example of passing an array for options

<?php

$options
= [
'options' => [
'min_range' => 10,
],
'flags' => FILTER_FLAG_ALLOW_OCTAL,
];

var_dump(filter_var('0755', FILTER_VALIDATE_INT, $options));
var_dump(filter_var('011', FILTER_VALIDATE_INT, $options));

?>

Yukarıdaki örneğin çıktısı:

int(493)
bool(false)

Örnek 4 Providing flags either directly or via an array

<?php

$str
= 'string';

var_dump(filter_var($str, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
var_dump(filter_var($str, FILTER_VALIDATE_BOOLEAN, ['flags' => FILTER_NULL_ON_FAILURE]));

?>

Yukarıdaki örneğin çıktısı:

NULL
NULL

Ayrıca Bakınız