How to add an option and a flag to filter_input

2019-07-04 07:04发布

问题:

How do you add both an option and a flag to filter_input? The PHP documentation says to use an associative array but doesn't give any examples on the correct syntax. I've tried various formats, including the following:

$textOpts = filter_input(INPUT_POST, "text", FILTER_SANITIZE_STRING, array("options" => FILTER_FORCE_ARRAY, "flags" => !FILTER_FLAG_ENCODE_LOW));
$textOpts = filter_input(INPUT_POST, "text", FILTER_SANITIZE_STRING, array("options" => array(FILTER_FORCE_ARRAY), "flags" => array(!FILTER_FLAG_ENCODE_LOW)));

I can't seem to get the syntax down, how do I write this?

回答1:

<form name="test" id="test" action="" method="POST">
<input type="text" name="demo[]" id="demo[]" value="">
<input type="submit" name="submit-bt" id="submit-bt" value="Submit">
</form>
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

$args = array('flags' => FILTER_REQUIRE_ARRAY, 
    'options'   => array('min_range' => 1, 'max_range' => 10)
);

if (!empty($_POST['submit-bt'])){
    $textOpts = filter_input(INPUT_POST, "demo", FILTER_VALIDATE_INT, $args);
    print_R($textOpts);exit;
}

?>