Filter filter_var

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

filter_var — 지정된 필터로 변수를 필터링합니다.


설명

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


매개변수

value
필터링할 값입니다. 스칼라 값은 필터링되기 전에 내부적으로 문자열로 변환됩니다.
filter
적용할 필터의 ID입니다. 필터 유형 매뉴얼 페이지에는 사용 가능한 필터가 나열되어 있습니다.

생략하면 FILTER_DEFAULT가 사용되며 이는 FILTER_UNSAFE_RAW와 동일합니다. 그러면 기본적으로 필터링이 수행되지 않습니다.

options
옵션의 연관 배열 또는 플래그의 비트 분리. 필터가 옵션을 허용하는 경우 배열의 "플래그" 필드에 플래그를 제공할 수 있습니다. "콜백" 필터의 경우 호출 가능한 유형이 전달되어야 합니다. 콜백은 하나의 인수, 즉 필터링할 값을 수락해야 하며 필터링/위생 처리한 후 값을 반환해야 합니다.
                       
<?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 filters that only accept flags, you can pass them directly
$var = filter_var('oops', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);

// for filters 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(", ", $value, 2);
    $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'));
?>
                       
                     

반환 값

필터링된 데이터를 반환하거나 필터가 실패하면 false를 반환합니다.


Examples

예제 #1 filter_var() 예제

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

위의 예는 다음을 출력합니다.

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

기타