<?php$filters = array //定义过滤的数组( ???"name" => array ???( ???????"filter"=>FILTER_SANITIZE_STRING ???), ???"age" => array ???( ???????"filter"=>FILTER_VALIDATE_INT, //下面的option是FILTER_VALIDATE_INT的过滤选项,查手册可以用到, ???????"options"=>array //每个过滤器都可用不同的过滤选项 ???????( ???????????"min_range"=>1, ???????????"max_range"=>120 ???????) ???), ???"email"=> FILTER_VALIDATE_EMAIL); $result = filter_input_array(INPUT_GET, $filters); ?//右边接入过滤的数组 if (!$result["age"]){ ???echo("年龄必须在 1 到 120 之间。<br>");}elseif(!$result["email"]){ ???echo("E-Mail 不合法<br>");}else{ ???echo("输入正确");}?>
自定义过滤器的方法:
<?phpfunction convertSpace($string){ ???return str_replace("_", ".", $string);} $string = "www_fucktworld_com!"; echo filter_var($string, FILTER_CALLBACK,array("options"=>"convertSpace"));?>
上面代码的结果如下所示:
www.fuckworld.com
上面的实例把所有 "_" 转换成 "." :
创建一个把 "_" 替换为 "." 的函数
调用 filter_var() 函数,它的参数是 FILTER_CALLBACK 过滤器以及包含我们的函数的数组
php强大的filter过滤用户输入
原文地址:https://www.cnblogs.com/drkang/p/8469186.html