我创建了以下代码,以更好地了解php过滤器。
它基本上针对2个整数过滤器验证年和周的2个特定url参数。
问题是,它不会通知您在BUT中正确键入了哪些参数,发现超出范围。目前是全部或全部;我无法告诉用户具体出了什么问题。
该var_dump($result)显示返回此:
array(2) {
["year"]=> int(2019)
["week"]=> bool(false)
}
谁能告诉我如何创建此通知?
这是代码。请记住,为了测试它,您必须在本地开发环境中进行测试。
/*
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
*/
// Custom URL PHP filters
$filters = [
'year' => [ 'filter' => FILTER_VALIDATE_INT, 'options' => ['min_range' => 2019, 'max_range' => 2025] ],
'week' => [ 'filter' => FILTER_VALIDATE_INT, 'options' => ['min_range' => 1, 'max_range' => 52 ] ]
];
/* Begin validation(s)... */
// At least 1 URL parameter is required.
if (!is_null($result = filter_input_array(INPUT_GET, $filters))) {
// If we made it this far...
// Search result array for any keys containing null or false
if(!in_array(null || false, $result)) {
// If all key/value pairs from array $filters validated successfuly...
echo 'Status: Validation success.<br>';
echo 'Reason: All URL parameters decleared in $filters are present';
echo '<br>';
echo 'and have been validated against their respective filters.';
echo '<br><br>';
echo '<hr>';
echo '<strong>$result</strong> output';
echo '<br><br>';
echo 'print_r:<br>';
print_r($result);
echo '<br><br>';
echo 'var_dump:<br>';
var_dump($result);
茅侃侃
扬帆大鱼