表单有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数组中的所有表单字段创建一个条目?当我对所有领域都有一条规则时呢?
答案 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!