简体   繁体   English

由filter_var中的运算符链接的php常量列表

[英]list of php constants linked by operator in filter_var

I created a class named Sanitize. 我创建了一个名为Sanitize的类。

I use the filter_var function to filter and sanitize data. 我使用filter_var函数来过滤和清理数据。 I built the filter_var() third parameter from an array: $options which is like the following code: 我从数组:$ options构建了第三个参数filter_var(),它类似于以下代码:

private $options = array('typeOfData' => array('options' => array(), 'flags' => array()));

I can build an array with the right options, but this method won't work. 我可以使用正确的选项构建一个数组,但是该方法不起作用。 I wanted only one of either option or flag, it wouldn't be a problem. 我只想要选项或标志之一,这不是问题。 Unfortunately, I'd like to pass several option or flag constants to filter_var. 不幸的是,我想将几​​个选项或标志常量传递给filter_var。

For the flags, is it possible to build the wanted result like FLAG1 | 对于标志,是否可以构建所需的结果,例如FLAG1 | FLAG2? FLAG2? If not, should I use filter_var, with a switch to use filter_var depending on the required flags? 如果没有,我是否应该使用filter_var,并根据所需的标志切换使用filter_var?

This is the current code using filter_var: filter_var($value, $this->filters[$typeOFData], $this->currentOptions); 这是使用filter_var的当前代码: filter_var($value, $this->filters[$typeOFData], $this->currentOptions);

A class that Sanitizes based on type: 根据类型进行消毒的类:

class Sanitize{
private $data;
private $filteredData;
private $options;
private $typeof;
private $filters;

function __contruct($data){

$this->typeof = gettype($data);
$this->data = $data;

switch ($this->typeof){
    case "integer":
       $this->options = array( /* array of options for integer type*/ );
       $this->filters = array( /*array of all filters you want to use on integers*/ )
        break;
    case "string":
       //....
}

private function getSanitized(){

$filtersNum = count($this->filters);

for($x=0;$x<$filtersNum;$x++){

if(allowsOptions($this->filters[$x])){ $options = $this->options; }else{$options = null;}

$this->filteredData = filter_var($this->data, $this->filters[$x], $options)
}
}

private function allowsOptions($filter){
/* all your code to check*/
return true or false
}
}

I hope this helps, If not, let me know. 希望对您有所帮助,否则请通知我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM