filter_input
(PHP 5 >= 5.2.0, PHP 7, PHP 8)
filter_input — 通過名稱獲取特定的外部變數,並且可以通過過濾器處理它
說明
參數
-
type -
INPUT_GET,INPUT_POST,INPUT_COOKIE,INPUT_SERVER或INPUT_ENV之一。 -
variable_name -
待獲取的變數名。
-
filter -
The ID of the filter to apply. The Types of filters manual page lists the available filters.
If omitted,
FILTER_DEFAULTwill be used, which is equivalent toFILTER_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 & son. <a href='?search=Me%20%26%20son'>Search again.</a>
參見
- filter_var() - 使用特定的過濾器過濾一個變數
- filter_input_array() - 獲取一系列外部變數,並且可以通過過濾器處理它們
- filter_var_array() - 獲取多個變數並且過濾它們
- Types of filters