filter_input_array - > FILTER_SANITIZE_NUMBER_FLOAT - >行动,和。在所有INPUT_POST字段中

时间:2013-12-28 20:04:37

标签: php arrays definition filter-input

表单有6个字段,除了和之外只允许使用数字。

现在我尝试使用filter_input_array进行检查。当我使用

filter_input_array(INPUT_POST, FILTER_SANITIZE_NUMBER_FLOAT);

脚本将0,5改为05。

当我这样尝试时:

$filter = array('filter' => FILTER_SANITIZE_NUMBER_FLOAT, 'flags' => FILTER_FLAG_ALLOW_FRACTION);
$perf['post'] = filter_input_array(INPUT_POST, $filter);

perf ['post']看起来像这样:

  

数组([filter] => [flags] =>)

所以我必须为$ filter数组中的所有表单字段创建一个条目?当我对所有领域都有一条规则时呢?

1 个答案:

答案 0 :(得分:2)

您目前无法将所需的标记(FILTER_FLAG_ALLOW_THOUSAND | FILTER_FLAG_ALLOW_FRACTION)添加到任何过滤器数组函数中,而无需单独指定每个字段;虽然很好。

备选方案:

指定每个字段

$filter = array(
    'field_one' => array(
        'filter' => FILTER_SANITIZE_NUMBER_FLOAT,
        'flags' => FILTER_FLAG_ALLOW_THOUSAND | FILTER_FLAG_ALLOW_FRACTION
    ),
    'field_two' => array(
        'filter' => FILTER_SANITIZE_NUMBER_FLOAT,
        'flags' => FILTER_FLAG_ALLOW_THOUSAND | FILTER_FLAG_ALLOW_FRACTION
    ),
    ...
);

重用定义数组

$my_number_def = array(
        'filter' => FILTER_SANITIZE_NUMBER_FLOAT,
        'flags' => FILTER_FLAG_ALLOW_THOUSAND | FILTER_FLAG_ALLOW_FRACTION
    );

$filter = array(
        'field_one' => $my_number_def,
        'field_two' => $my_number_def,
        ...
    );

独立

$args = FILTER_FLAG_ALLOW_THOUSAND | FILTER_FLAG_ALLOW_FRACTION;

$perf['post']['one'] = filter_input(INPUT_POST, 'postOne', FILTER_SANITIZE_NUMBER_FLOAT, $args);
$perf['post']['two'] = filter_input(INPUT_POST, 'postTwo', FILTER_SANITIZE_NUMBER_FLOAT, $args);

手动

正则表达式:

$perf['post'] = preg_grep('/^[0-9,.]$/', $_POST); // unverified regex!