使用过滤器验证年份和星期。如何通知特定的超出范围的参数?

我创建了以下代码,以更好地了解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);

慕田峪9158850
浏览 152回答 2
2回答

茅侃侃

根据文档,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 fieldsforeach($failed as $k=>$v) {&nbsp; &nbsp; $msg = ($v === null) ? "Field %s was missing\n" : "Field %s failed validation\n";&nbsp; &nbsp; printf($msg, $k);}输出:Field week failed validationField day was missing

扬帆大鱼

每个想了解如何使用PHP过滤器处理url参数的人的样本。再次感谢迈克!源代码<?php/*URL Samples to trycode.php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- ERRORcode.php?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - ERRORcode.php?year=2018&week=0&nbsp; - OUT OF RANGEcode.php?year=2018&week=1&nbsp; - OUT OF RANGEcode.php?year=2019&week=1&nbsp; - IN RANGEcode.php?year=2019&week=52 - IN RANGEcode.php?year=2019&week=0&nbsp; - OUT OF RANGEcode.php?year=2019&week=53 - OUT OF RANGEcode.php?year=2026&week=1&nbsp; - OUT OF RANGEcode.php?year=2026&week=52 - OUT OF RANGE*/// PREPARE FILTER$FILTER_VALIDATE_INT_RANGE_YEAR = [&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filter'&nbsp; => FILTER_VALIDATE_INT,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'options' => [ 'min_range' => 2019, 'max_range' => 2025 ]&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];// PREPARE FILTER$FILTER_VALIDATE_INT_RANGE_WEEK = [&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filter'&nbsp; => FILTER_VALIDATE_INT,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'options' => [ 'min_range' => 1,&nbsp; &nbsp; 'max_range' => 52&nbsp; &nbsp;]&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];// PREPARE FILTER$FILTER_VALIDATE_INT_RANGE_DAYS = [&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'filter'&nbsp; => FILTER_VALIDATE_INT,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'options' => [ 'min_range' => 1,&nbsp; &nbsp; 'max_range' => 365&nbsp; ]&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];// ACTIVATE FILTERS$FILTERS_ACTIVE = [ // 'url_parameter' => php filter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'year' => $FILTER_VALIDATE_INT_RANGE_YEAR,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'week' => $FILTER_VALIDATE_INT_RANGE_WEEK,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'day'&nbsp; => $FILTER_VALIDATE_INT_RANGE_DAYS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];/*&nbsp; ABOUT filter_input_array&nbsp; According to the PHP documentation, filter_input_array returns an array containing&nbsp;&nbsp; the values of the requested variables on completion.&nbsp; In order for it to work, at least 1 URL parameter must be correctly formed or entered.&nbsp; 1. A $VALIDATE variable will be entirely NULL if there isn't at least 1 url_parameter correctly formed or entered.&nbsp; 2. A $VALIDATE array key=>value will be NULL&nbsp; if that particular filter parameter is not set.&nbsp; 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 ) ) ) {&nbsp; /*&nbsp; &nbsp; ABOUT array_filter&nbsp; &nbsp; According to the PHP documentation, array_filter filters elements of an array&nbsp;&nbsp; &nbsp; using an optional callback function.&nbsp; &nbsp; If no callback is supplied, all key=>value pairs of $VALIDATE equal to FALSE will be removed.&nbsp;&nbsp; */&nbsp; // ARRAY FILTER - PASSED&nbsp; // int | key=>value pairs&nbsp; $INT_ARRAY_FILTER&nbsp; &nbsp;= array_filter( $VALIDATE, 'is_int' );&nbsp; // ARRAY FILTER - UNSET&nbsp; // null | key=>value pairs&nbsp; $NULL_ARRAY_FILTER&nbsp; = array_filter( $VALIDATE, function($value) { return $value === null;&nbsp; } );&nbsp; // ARRAY FILTER - FAILED&nbsp; // false | key=>value pairs&nbsp; $FALSE_ARRAY_FILTER = array_filter( $VALIDATE, function($value) { return $value === false; } );&nbsp; echo '<h3>URL Input</h3>';&nbsp; // VALIDATE - PASS&nbsp; // LOOP through INT_ARRAY_FILTER - array&nbsp; foreach( $INT_ARRAY_FILTER as $key => $value ) {&nbsp; &nbsp; // integer in filter range&nbsp; &nbsp; $msg = "URL Parameter <strong style=\"color:green;\"><code>%s=%s</code></strong> | in filter range.<br>";&nbsp; &nbsp; // RENDER&nbsp; &nbsp; printf( $msg, $key, $value );&nbsp; }&nbsp; // VALIDATE - UNSET&nbsp; // LOOP through NULL_ARRAY_FILTER - array&nbsp; foreach( $NULL_ARRAY_FILTER as $key => $value ) {&nbsp; &nbsp; // null = unset&nbsp; &nbsp; $msg = "URL Parameter <strong style=\"color:darkorange;\"><code>%s=</code></strong> | unset.<br>";&nbsp; &nbsp; // RENDER&nbsp; &nbsp; printf( $msg, $key, $value );&nbsp; }&nbsp; // VALIDATE - FAIL&nbsp; // LOOP through FALSE_ARRAY_FILTER - array&nbsp; foreach( $FALSE_ARRAY_FILTER as $key=>$value ) {&nbsp; &nbsp; // null = unset&nbsp; &nbsp; $msg = "URL Parameter <strong style=\"color:red;\"><code>%s=</code></strong> | out of filter range.<br>";&nbsp; &nbsp; // RENDER&nbsp; &nbsp; printf( $msg, $key, $value );&nbsp; }&nbsp; echo '<br>';&nbsp; echo '<hr>';&nbsp; echo '<h3>Output</h3>';&nbsp; echo 'var_dump (<strong style="color:blue;">$VALIDATE</strong>):';&nbsp; echo '<br>';&nbsp; var_dump( $VALIDATE );&nbsp; echo '<br><br>';&nbsp; echo 'var_dump (<strong style="color:green;">$INT_ARRAY_FILTER</strong>):';&nbsp; echo '<br>';&nbsp; var_dump( $INT_ARRAY_FILTER );&nbsp; echo '<br><br>';&nbsp; echo 'var_dump (<strong style="color:darkorange;">$NULL_ARRAY_FILTER</strong>):';&nbsp; echo '<br>';&nbsp; var_dump( $NULL_ARRAY_FILTER );&nbsp; echo '<br><br>';&nbsp; echo 'var_dump (<strong style="color:red;">$FALSE_ARRAY_FILTER</strong>):';&nbsp; echo '<br>';&nbsp; var_dump( $FALSE_ARRAY_FILTER );&nbsp; echo '<br><br>';// Outter IF} else {&nbsp; // $VALIDATE = NULL&nbsp; echo '<h3>URL Input Error</h3>';&nbsp; echo 'URL Parameter(s) <strong>not present</strong> in URL | missing.';&nbsp; echo '<br>';&nbsp; echo 'At least 1 URL parameter is required!';&nbsp; echo '<br><br>';&nbsp; echo '<strong>URL Example:</strong> <code>index.php<strong>?year=2019</strong></code>';&nbsp; echo '<br><br>';&nbsp; echo '<hr>';&nbsp; echo '<h3>Output</h3>';&nbsp; echo 'var_dump (<strong style="color:blue;">$VALIDATE</strong>):';&nbsp; echo '<br>';&nbsp; var_dump( $VALIDATE );&nbsp; echo '<br><br>';}?>
打开App,查看更多内容
随时随地看视频慕课网APP