PHP.nl

filter_var

filter_var

Filters a variable with a specified filter

mixed **filter_var** mixed $value int $filter  $options

Filter a variable using a

validation filters, a

sanitization filters, or a custom filter. FILTER_VALIDATE_*``FILTER_SANITIZE_*

valueValue to filter.

Waarschuwing: > Scalar values are

   internally before they are filtered.
  converted to string

filter The filter to apply. Can be a validation filter by using one of the

  constants, a sanitization filter by using one of the
  
  or , or a custom filter by using
  .
 `FILTER_VALIDATE_*``FILTER_SANITIZE_*``FILTER_UNSAFE_RAW``FILTER_CALLBACK`

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

options Either an associative of options, or a bitmask of filter flag constants . array``FILTER_FLAG_*

  If the  accepts options,
  flags can be provided by using the  field of array.
 `filter``"flags"`

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

Voorbeeld: A 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));
?>
string(15) "bob@example.com"
bool(false)

Voorbeeld: 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));
?>
array(3) {
  [0]=>
  string(15) "bob@example.com"
  [1]=>
  string(18) "test@example.local"
  [2]=>
  bool(false)
}

**Voorbeeld: Example of passing an array for **

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

?>
int(493)
bool(false)

**Voorbeeld: Providing flags either directly or via an **

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

?>
NULL
NULL

filter_var_array``filter_input``filter_input_array``FILTER_VALIDATE_*``FILTER_SANITIZE_*