2 回答
TA贡献1842条经验 获得超21个赞
根据文档,filter_input_array返回“成功时包含所请求变量的值的数组。FALSE如果过滤器失败或未NULL设置变量,则为数组值。” 因此,您所需要做的就是在数组中搜索falseor null:
<?php
// sample data
$result = ["year"=>2019, "week"=>false, "day"=>null];
// remove non-false/null values
$failed = array_filter($result, function($v){return $v===false || $v===null;});
// now loop through the failed fields
foreach($failed as $k=>$v) {
$msg = ($v === null) ? "Field %s was missing\n" : "Field %s failed validation\n";
printf($msg, $k);
}
输出:
Field week failed validation
Field day was missing
TA贡献1799条经验 获得超9个赞
每个想了解如何使用PHP过滤器处理url参数的人的样本。再次感谢迈克!
源代码
<?php
/*
URL Samples to try
code.php - ERROR
code.php? - ERROR
code.php?year=2018&week=0 - OUT OF RANGE
code.php?year=2018&week=1 - OUT OF RANGE
code.php?year=2019&week=1 - IN RANGE
code.php?year=2019&week=52 - IN RANGE
code.php?year=2019&week=0 - OUT OF RANGE
code.php?year=2019&week=53 - OUT OF RANGE
code.php?year=2026&week=1 - OUT OF RANGE
code.php?year=2026&week=52 - OUT OF RANGE
*/
// PREPARE FILTER
$FILTER_VALIDATE_INT_RANGE_YEAR = [
'filter' => FILTER_VALIDATE_INT,
'options' => [ 'min_range' => 2019, 'max_range' => 2025 ]
];
// PREPARE FILTER
$FILTER_VALIDATE_INT_RANGE_WEEK = [
'filter' => FILTER_VALIDATE_INT,
'options' => [ 'min_range' => 1, 'max_range' => 52 ]
];
// PREPARE FILTER
$FILTER_VALIDATE_INT_RANGE_DAYS = [
'filter' => FILTER_VALIDATE_INT,
'options' => [ 'min_range' => 1, 'max_range' => 365 ]
];
// ACTIVATE FILTERS
$FILTERS_ACTIVE = [ // 'url_parameter' => php filter
'year' => $FILTER_VALIDATE_INT_RANGE_YEAR,
'week' => $FILTER_VALIDATE_INT_RANGE_WEEK,
'day' => $FILTER_VALIDATE_INT_RANGE_DAYS
];
/*
ABOUT filter_input_array
According to the PHP documentation, filter_input_array returns an array containing
the values of the requested variables on completion.
In order for it to work, at least 1 URL parameter must be correctly formed or entered.
1. A $VALIDATE variable will be entirely NULL if there isn't at least 1 url_parameter correctly formed or entered.
2. A $VALIDATE array key=>value will be NULL if that particular filter parameter is not set.
3. A $VALIDATE array key=>value will be FALSE if that particular filter fails.
*/
// EXECUTE FILTERS
// If we have at least 1 URL parameter correctly formed or entered...
if ( !is_null( $VALIDATE = filter_input_array( INPUT_GET, $FILTERS_ACTIVE ) ) ) {
/*
ABOUT array_filter
According to the PHP documentation, array_filter filters elements of an array
using an optional callback function.
If no callback is supplied, all key=>value pairs of $VALIDATE equal to FALSE will be removed.
*/
// ARRAY FILTER - PASSED
// int | key=>value pairs
$INT_ARRAY_FILTER = array_filter( $VALIDATE, 'is_int' );
// ARRAY FILTER - UNSET
// null | key=>value pairs
$NULL_ARRAY_FILTER = array_filter( $VALIDATE, function($value) { return $value === null; } );
// ARRAY FILTER - FAILED
// false | key=>value pairs
$FALSE_ARRAY_FILTER = array_filter( $VALIDATE, function($value) { return $value === false; } );
echo '<h3>URL Input</h3>';
// VALIDATE - PASS
// LOOP through INT_ARRAY_FILTER - array
foreach( $INT_ARRAY_FILTER as $key => $value ) {
// integer in filter range
$msg = "URL Parameter <strong style=\"color:green;\"><code>%s=%s</code></strong> | in filter range.<br>";
// RENDER
printf( $msg, $key, $value );
}
// VALIDATE - UNSET
// LOOP through NULL_ARRAY_FILTER - array
foreach( $NULL_ARRAY_FILTER as $key => $value ) {
// null = unset
$msg = "URL Parameter <strong style=\"color:darkorange;\"><code>%s=</code></strong> | unset.<br>";
// RENDER
printf( $msg, $key, $value );
}
// VALIDATE - FAIL
// LOOP through FALSE_ARRAY_FILTER - array
foreach( $FALSE_ARRAY_FILTER as $key=>$value ) {
// null = unset
$msg = "URL Parameter <strong style=\"color:red;\"><code>%s=</code></strong> | out of filter range.<br>";
// RENDER
printf( $msg, $key, $value );
}
echo '<br>';
echo '<hr>';
echo '<h3>Output</h3>';
echo 'var_dump (<strong style="color:blue;">$VALIDATE</strong>):';
echo '<br>';
var_dump( $VALIDATE );
echo '<br><br>';
echo 'var_dump (<strong style="color:green;">$INT_ARRAY_FILTER</strong>):';
echo '<br>';
var_dump( $INT_ARRAY_FILTER );
echo '<br><br>';
echo 'var_dump (<strong style="color:darkorange;">$NULL_ARRAY_FILTER</strong>):';
echo '<br>';
var_dump( $NULL_ARRAY_FILTER );
echo '<br><br>';
echo 'var_dump (<strong style="color:red;">$FALSE_ARRAY_FILTER</strong>):';
echo '<br>';
var_dump( $FALSE_ARRAY_FILTER );
echo '<br><br>';
// Outter IF
} else {
// $VALIDATE = NULL
echo '<h3>URL Input Error</h3>';
echo 'URL Parameter(s) <strong>not present</strong> in URL | missing.';
echo '<br>';
echo 'At least 1 URL parameter is required!';
echo '<br><br>';
echo '<strong>URL Example:</strong> <code>index.php<strong>?year=2019</strong></code>';
echo '<br><br>';
echo '<hr>';
echo '<h3>Output</h3>';
echo 'var_dump (<strong style="color:blue;">$VALIDATE</strong>):';
echo '<br>';
var_dump( $VALIDATE );
echo '<br><br>';
}
?>
- 2 回答
- 0 关注
- 151 浏览
添加回答
举报