filter_var

(PHP 5 >= 5.2.0, PHP 7)

filter_var使锟斤拷锟截讹拷锟侥癸拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷

说锟斤拷

filter_var ( mixed $variable [, int $filter = FILTER_DEFAULT [, mixed $options ]] ) : mixed

锟斤拷锟斤拷

variable

锟斤拷锟斤拷锟剿的憋拷锟斤拷锟斤拷注锟解:锟斤拷锟斤拷锟斤拷值锟节癸拷锟斤拷前锟斤拷锟结被转锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷

filter

The ID of the filter to apply. The Types of filters manual page lists the available filters.

If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. This will result in no filtering taking place by default.

options

一锟斤拷选锟斤拷墓锟斤拷锟斤拷锟斤拷椋拷锟斤拷甙锟轿伙拷锟斤拷值谋锟绞撅拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷选锟筋,锟斤拷锟斤拷通锟斤拷锟斤拷锟斤拷锟� "flags" 位去锟结供锟斤拷些锟斤拷示锟斤拷 锟斤拷锟节回碉拷锟酵的癸拷锟斤拷锟斤拷锟斤拷应锟矫达拷锟斤拷 callable锟斤拷锟斤拷锟斤拷氐锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟剿碉拷值锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷一锟斤拷锟节癸拷锟斤拷/锟斤拷锟斤拷锟斤拷锟街碉拷锟�

<?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 filter that only accept flags, you can pass them directly
$var filter_var('oops'FILTER_VALIDATE_BOOLEANFILTER_NULL_ON_FAILURE);

// for filter 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(", "$value2);
    
$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'));
?>

锟斤拷锟斤拷值

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

锟斤拷锟斤拷

Example #1 一锟斤拷 filter_var() 锟斤拷锟斤拷锟斤拷

<?php
var_dump
(filter_var('bob@example.com'FILTER_VALIDATE_EMAIL));
var_dump(filter_var('http://example.com'FILTER_VALIDATE_URLFILTER_FLAG_PATH_REQUIRED));
?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

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

锟轿硷拷

  • filter_var_array() - 锟斤拷取锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷夜锟斤拷锟斤拷锟斤拷锟�
  • filter_input() - 通锟斤拷锟斤拷锟狡伙拷取锟截讹拷锟斤拷锟解部锟斤拷锟斤拷锟斤拷锟斤拷锟揭匡拷锟斤拷通锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
  • filter_input_array() - 锟斤拷取一系锟斤拷锟解部锟斤拷锟斤拷锟斤拷锟斤拷锟揭匡拷锟斤拷通锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
  • Types of filters
  • callback 锟斤拷锟酵碉拷锟斤拷息