PHP函数参考30-Filter过滤器扩展函数

  • Filter介绍
  • Filter函数
  • Filter过滤器类型



原文:
PHP函数参考30-Filter过滤器扩展函数 - 9ong
PHP函数参考31-Reflection反射 - 9ong

Filter介绍

该Filter扩展通过验证或清理数据来筛选数据。当数据源包含未知(或外部)数据(如用户提供的输入)时,这特别有用。例如,该数据可能来自HTML表单。

过滤主要有两种:验证和清洗。

验证是用来验证或检查数据是否满足某些条件。例如,传入FILTER_VALIDATE_EMAIL将确定数据是否是有效的电子邮件地址,但不会改变数据本身。

清洗会对数据进行清洗,因此它可以通过删除不需要的字符来改变数据。例如,传入filter_sanitie_email将删除不适合电子邮件地址包含的字符。也就是说,它不能验证数据。

Filter函数

  • filter_list — 返回所支持的过滤器列表

    Array
    (
        [0] => int
        [1] => boolean
        [2] => float
        [3] => validate_regexp
        [4] => validate_domain
        [5] => validate_url
        [6] => validate_email
        [7] => validate_ip
        [8] => validate_mac
        [9] => string
        [10] => stripped
        [11] => encoded
        [12] => special_chars
        [13] => full_special_chars
        [14] => unsafe_raw
        [15] => email
        [16] => url
        [17] => number_int
        [18] => number_float
        [19] => magic_quotes
        [20] => callback
    )
    
    
  • filter_id — 返回与某个特定名称的过滤器相关联的id

  • filter_has_var — 检测是否存在指定类型的变量

    检查这些输入:INPUT_GET、 INPUT_POST、 INPUT_COOKIE、 INPUT_SERVER、 INPUT_ENV 里的其中一个的变量。

    var_dump(filter_has_var(INPUT_GET, "name"));
    
  • filter_var — 使用特定的过滤器过滤一个变量

    filter_var( mixed $variable[, int $filter = FILTER_DEFAULT[, mixed $options]] ) : mixed
    
    • variable

      待过滤变量

    • filter

      过滤器名称。PHP: Types of filters - Manual

    • options

      过滤器选项。就是过滤规则设置。主要由 Validate filters options 与 Filter flags 设置过滤规则。

  • filter_var_array — 获取多个变量并且过滤它们

    不需要重复调用 filter_var() 就能获取多个变量。

    filter_var数组形式,输入变量数组,通过过滤器数组进行相应变量的过滤得到新的变量数组。

    现代web框架都有封装各自的类似机制的filter过滤器。

    error_reporting(E_ALL | E_STRICT);
    $data = array(
        'product_id'    => 'libgd
                        
                        

你可能感兴趣的:(PHP函数参考30-Filter过滤器扩展函数)