Hàm filter_input_array() trong PHP
Ví dụ
Sử dụng hàm filter_input_array() để lọc ba biến POST. Các biến POST nhận được là tên, tuổi và e-mail:
<?php
$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"=>120)
),
"email" => FILTER_VALIDATE_EMAIL
);
print_r(filter_input_array(INPUT_POST, $filters));
?>
Đầu ra của đoạn mã trên sẽ là:
Định nghĩa và cách sử dụng
Hàm filter_input_array() lấy các biến bên ngoài (ví dụ: từ dữ liệu nhập vào biểu mẫu) và lọc chúng theo tùy chọn.
Hàm này hữu ích để truy xuất/lọc nhiều giá trị thay vì gọi filter_input() nhiều lần.
Cú pháp
filter_input_array( type, definition, add_empty )
Giá trị tham số
Parameter | Description |
---|---|
type | Required. The input type to check for. Can be one of the following:
|
definition | Optional. Specifies an array of filter arguments. A valid array key is a variable name, and a valid value is a filter name or ID, or an array specifying the filter, flags and options. This parameter can also be a single filter name/ID; then all values in the input array are filtered by the specified filter |
add_empty | Optional. A Boolean value. TRUE adds missing keys as NULL to the return value. Default value is TRUE |
Chi tiết kỹ thuật
Giá trị trả về: | Một mảng có các giá trị của các biến thành công, FALSE nếu thất bại |
---|---|
Phiên bản PHP: | 5.2+ |
Nhật ký thay đổi PHP: | PHP 5.4 - Tham số add_empty đã được thêm |
❮ Tham khảo bộ lọc PHP hoàn chỉnh