为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕田峪9158850 2021-04-07 17:19:47
我创建了以下代码,以更好地了解php过滤器。它基本上针对2个整数过滤器验证年和周的2个特定url参数。问题是,它不会通知您在BUT中正确键入了哪些参数,发现超出范围。目前是全部或全部;我无法告诉用户具体出了什么问题。该var_dump($result)显示返回此:array(2) {  ["year"]=> int(2019)  ["week"]=> bool(false)}谁能告诉我如何创建此通知?这是代码。请记住,为了测试它,您必须在本地开发环境中进行测试。/*URL Samples to trycode.php                   - ERRORcode.php?                  - ERRORcode.php?year=2018&week=0  - OUT OF RANGEcode.php?year=2018&week=1  - OUT OF RANGEcode.php?year=2019&week=1  - IN RANGEcode.php?year=2019&week=52 - IN RANGEcode.php?year=2019&week=0  - OUT OF RANGEcode.php?year=2019&week=53 - OUT OF RANGEcode.php?year=2026&week=1  - OUT OF RANGEcode.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);
查看完整描述

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


查看完整回答
反对 回复 2021-04-23
?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

每个想了解如何使用PHP过滤器处理url参数的人的样本。再次感谢迈克!

//img1.sycdn.imooc.com//6082574a00013fda04900406.jpg

源代码


<?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>';


}


?>

查看完整回答
反对 回复 2021-04-23
  • 2 回答
  • 0 关注
  • 151 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信