filter_input

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

filter_input通过名称获取特定的外部变量,并且可以通过过滤器处理它

说明

filter_input ( int $type , string $variable_name , int $filter = FILTER_DEFAULT , mixed $options = ? ) : mixed

参数

type

INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVERINPUT_ENV之一。

variable_name

待获取的变量名。

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" 位去提供这些标示。

返回值

如果成功的话返回所请求的变量。如果过滤失败则返回 false ,如果variable_name 不存在的话则返回 null 。 如果标示 FILTER_NULL_ON_FAILURE 被使用了,那么当变量不存在时返回 false ,当过滤失败时返回 null

范例

示例 #1 一个 filter_input() 的例子

<?php
$search_html 
filter_input(INPUT_GET'search'FILTER_SANITIZE_SPECIAL_CHARS);
$search_url filter_input(INPUT_GET'search'FILTER_SANITIZE_ENCODED);
echo 
"You have searched for $search_html.\n";
echo 
"<a href='?search=$search_url'>Search again.</a>";
?>

以上例程的输出类似于:

You have searched for Me &#38; son.
<a href='?search=Me%20%26%20son'>Search again.</a>

参见