PHP 过滤器函数

PHP filter_input_array() 函数获取外部变量并选择性地过滤它们。此函数对于检索许多值非常有用,而无需重复调用 filter_input()

语法

filter_input_array(type, options, add_empty) 

    参数

    type必填。 指定输入类型。它可以是以下之一:
    • INPUT_GET
    • INPUT_POST
    • INPUT_COOKIE
    • INPUT_SERVER
    • INPUT_ENV
    options可选。 指定过滤器参数数组。有效的数组键是变量名称,有效值是过滤器名称或 ID,或者指定过滤器、标志和选项的数组。该参数也可以是单个过滤器名称/ID;然后输入数组中的所有值都由指定的过滤器过滤。请参阅过滤器列表
    add_empty可选。 如果设置为 true,则将缺失的键作为 null 添加到返回值中。默认值为 true。

    返回值

    成功时返回包含所请求变量值的数组。如果 type 指定的输入数组未填充,且未给出 FILTER_NULL_ON_FAILURE 标志,则该函数返回 null,否则返回 false。对于其他失败,返回 false。

    如果过滤器失败,则数组值为 false;如果未设置变量,则数组值为 null。或者,如果使用标志 FILTER_NULL_ON_FAILURE,则如果未设置变量,则返回 false;如果过滤器失败,则返回 null。如果add_empty参数为false,则不会为未设置的变量添加数组元素。

    示例:过滤POST变量

    在下面的示例中,filter_input_array() 函数用于过滤三个 POST 变量。接收到的 POST 变量是 'name'、'age' 和 'email'。

    <?php
    /*considering this data coming from POST
    $_POST = array(
      'name'  => 'John Smith',
      'age'   =>  25,
      'email' => 'John@example.com',
    );
    */
    
    $filters = array (
      "name"  => array ("filter"=>FILTER_CALLBACK,
                        "flags"=>FILTER_FORCE_ARRAY,
                        "options"=>"ucwords"
                       ),
      "age"   => array ("filter"=>FILTER_VALIDATE_INT,
                        "options"=>array("min_range"=>1,
                                         "max_range"=>100)
                       ),
      "email" => FILTER_VALIDATE_EMAIL
    );
    
    print_r(filter_input_array(INPUT_POST, $filters));
    ?> 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    上述代码的输出将是:

    Array
    (
        [name] => John Smith
        [age] => 25
        [email] => John@example.com
    ) 
    • 1
    • 2
    • 3
    • 4
    • 5

    示例:filter_input_array( ) 示例

    再考虑一个示例,其中该函数用于验证和清理来自 POST 方法的数据。

    <?php
    /*considering this data coming from POST
    $_POST = array(
      'product_id' => 'libgd<script>',
      'component'  => array('10'),
      'version'    => '2.0.33',
      'testarray'  => array('2', '23', '10', '12'),
      'testscalar' => '2',
    );
    */
    
    $args = array(
      'product_id'  => FILTER_SANITIZE_ENCODED,
      'component'    => array('filter'  => FILTER_VALIDATE_INT,
                              'flags'   => FILTER_REQUIRE_ARRAY, 
                              'options' => array('min_range' => 1, 
                                                'max_range' => 10)
                             ),
      'version'      => FILTER_SANITIZE_ENCODED,
      'doesnotexist' => FILTER_VALIDATE_INT,
      'testscalar'   => array('filter' => FILTER_VALIDATE_INT,
                              'flags'  => FILTER_REQUIRE_SCALAR,
                             ),
      'testarray'    => array('filter' => FILTER_VALIDATE_INT,
                              'flags'  => FILTER_REQUIRE_ARRAY,
                             )
    );
    
    $myinputs = filter_input_array(INPUT_POST, $args);
    
    var_dump($myinputs);
    ?> 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    上述代码的输出将是:

    array(6) {
      ["product_id"]=>
      string(17) "libgd%3Cscript%3E"
      ["component"]=>
      array(1) {
        [0]=>
        int(10)
      }
      ["version"]=>
      string(6) "2.0.33"
      ["doesnotexist"]=>
      NULL
      ["testscalar"]=>
      int(2)
      ["testarray"]=>
      array(4) {
        [0]=>
        int(2)
        [1]=>
        int(23)
        [2]=>
        int(10)
        [3]=>
        int(12)
      }
    } 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25