PHP Filter相关函数

一直以来,都是用正则表达式来进行email的格式验证,却不知道PHP本身有内置的过滤方法,在此记录一下,以免忘了。

PHP Filter 简介

PHP 过滤器用于对来自非安全来源的数据(比如用户输入)进行验证和过滤。

 

PHP  Filter相关函数

http://jp2.php.net/manual/en/ref.filter.php

http://www.w3school.com.cn/php/php_ref_filter.asp

 

PHP  Filter相关函数
<?php

class Filter 
{
    const STRING = 1;
    const ENCODED = 2;
    const QUOTED = 3;
    const INTVAL = 4;
    const FLOAT = 5;
    const URL = 6;
    const EMAIL = 7;
    const IP = 8;
    const BOOLEAN = 9;

    public function __construct($data = array())
    {
       
    }

    public function exists($key)
    {
        return isset($this->data[$key]);
    }

    public function get($key, $type = null, $default = null, $max = null)
    {
        $input = trim($this->data[$key]);
        $input = self::data($input, $type, $default, $max);
        return $input;
    }

    public function getArray($key, $type = null, $default = null, $max = null, $delimiter = ‘,‘)
    {
        $data = array();
        $inputs = trim($this->data[$key]);
        $inputs = explode($delimiter, $inputs);
        foreach ($inputs as $input) {
            $data[] = self::data($input, $type, $default, $max);
        }
        $data = array_filter($data);
        return $data;
    }

    public static function data($input, $type = null, $default = null, $max = null)
    {
        switch ($type) {
            case self::STRING:
                return self::string($input, $default);
                break;
            case self::ENCODED:
                return self::email($input, $default);
                break;
            case self::QUOTED:
                return self::quoted($input, $default);
                break;
            case self::INTVAL:
                return self::intval($input, $default, $max);
                break;
            case self::FLOAT:
                return self::float($input, $default, $max);
                break;
            case self::URL:
                return self::url($input, $default);
                break;
            case self::EMAIL:
                return self::email($input, $default);
                break;
            case self::IP:
                return self::ip($input, $default);
                break;
            case self::BOOLEAN:
                return self::boolean($input, $default);
                break;
            default :
                return $input;
        }
        return $input;
    }

    public static function string($input, $default = null)
    {
        $input = (!empty($input) OR is_null($default)) ? trim($input) : $default;
        return htmlspecialchars($input);
        //return filter_var($input, FILTER_SANITIZE_SPECIAL_CHARS);
        //return filter_var($input, FILTER_SANITIZE_STRING);
    }

    public static function encoded($input, $default = null)
    {
        $input = (!empty($input) OR is_null($default)) ? trim($input) : $default;
        return filter_var($input, FILTER_SANITIZE_SPECIAL_CHARS);
    }

    public static function quoted($input, $default = null)
    {
        $input = (!empty($input) OR is_null($default)) ? trim($input) : $default;
        return filter_var($input, FILTER_SANITIZE_MAGIC_QUOTES);
    }

    public static function intval($input, $default = null, $max = null)
    {
        $input = (!empty($input) OR is_null($default)) ? trim($input) : $default;
        $result = filter_var($input, FILTER_SANITIZE_NUMBER_INT);
        if ($max && $result > $max) {
            $result = intval($max);
        }
        return $result;
    }

    public static function float($input, $default = null)
    {
        $input = (!empty($input) OR is_null($default)) ? trim($input) : $default;
        return filter_var($input, FILTER_SANITIZE_NUMBER_FLOAT);
    }

    public static function url($input, $default = null)
    {
        $input = (!empty($input) OR is_null($default)) ? trim($input) : $default;
        return filter_var($input, FILTER_SANITIZE_URL);
    }

    public static function email($input, $default = null)
    {
        $input = (!empty($input) OR is_null($default)) ? trim($input) : $default;
        return filter_var($input, FILTER_SANITIZE_EMAIL);
    }

    public static function ip($input, $default = null)
    {
        $input = (!empty($input) OR is_null($default)) ? trim($input) : $default;
        return $input;
    }

    public static function boolean($input, $default = null)
    {
        $input = (!empty($input) OR is_null($default)) ? trim($input) : $default;
        return filter_var($input, FILTER_VALIDATE_BOOLEAN);
    }

    public static function htmlToPlain($html, $decodeHtml = true, $keepFullTag = false)
    {
        if (!$keepFullTag) {
            $html = preg_replace(array(
                ‘/<head\b[^>]*>.*?<\/head\b[^>]*>/si‘,
                ‘/<style\b[^>]*>.*?<\/style[^>]*>/si‘,
                ‘/<(object|script|iframe|frame(set)?)\b[^>]*>.*?<\/\1\b[^>]*>/si‘,
                    ), ‘‘, $html);
        }
        $s = preg_replace(‘/<br\s*\/?>/i‘, "\n", $html);
        $s = strip_tags($s);
        if ($decodeHtml) {
            $s = htmlspecialchars_decode($s);
        }
        $s = preg_replace(‘/&nbsp;?/i‘, ‘ ‘, $s);
        return $s;
    }

    public static function plainToHtml($plain)
    {
        $s = htmlspecialchars($plain);
        $s = preg_replace(‘/[\r\n]+/‘, ‘<br/>‘, $s);
        return $s;
    }

}
PHP  Filter相关函数

PHP Filter相关函数,布布扣,bubuko.com

PHP Filter相关函数

上一篇:设置div中文字超出时自动换行


下一篇:jquery.validate.js 一个jQuery验证格式控件