整理了一批php中使用preg_match进行内容判断的函数做成了一个类,可以直接用,涵盖了大部分常用的判断,各位看官慢慢欣赏~代码如下

< ?php
/**
 *
 * @author Chen.Zhidong
 * 2012-1-17
 *
 */

class Validate{
    public static function isEmail($email){
        return preg_match('/^[a-z0-9!#$%&\'*+\/=?^`{}|~_-]+[.a-z0-9!#$%&\'*+\/=?^`{}|~_-]*@[a-z0-9]+[._a-z0-9-]*\.[a-z0-9]+$/ui', $email);
    }

    public static function isMD5($md5){
        return preg_match('/^[a-z0-9]{32}$/ui', $md5);
    }

    public static function isSHA1($sha1){
        return preg_match('/^[a-z0-9]{40}$/ui', $sha1);
    }

    public static function isPasswd($passwd,$size=6){
        return preg_match('/^[.a-z_0-9-!@#$%\^&*()]{'.$size.',32}$/ui', $passwd);
    }

    public static function isName($name){
        return preg_match('/^[^0-9!<>,;?=+()@#"°{}_$%:]*$/ui', stripslashes($name));
    }

    public static function isFloat($float){
        return strval(floatval($float)) == strval($float);
    }

    public static function isUnsignedFloat($float){
        return strval(floatval($float)) == strval($float) && $float >= 0;
    }

    public static function isOptFloat($float){
        return empty($float) || self::isFloat($float);
    }

    public static function isInt($value){
        return ((string)(int)$value === (string)$value || $value === false);
    }

    public static function isUnsignedInt($value){
        return (self::isInt($value) && $value < 4294967296 && $value >= 0);
    }

    public static function isUrl($url){
        return preg_match('/^([[:alnum:]]|[:#%&_=\(\)\.\? \+\-@\/])+$/ui', $url);
    }

    public static function isAbsoluteUrl($url){
        if (!empty($url))
            return preg_match('/^https?:\/\/([[:alnum:]]|[:#%&_=\(\)\.\? \+\-@\/\$])+$/ui', $url);
        return true;
    }

    public static function isSortDirection($value){
        return (!is_null($value) && ($value === 'ASC' || $value === 'DESC'));
    }

    public static function isImageFile($filename){
        return preg_match('/^[a-z0-9_-]+\.[jpg|jpeg|png|bmp|gif]$/ui', $filename);
    }

    public static function isString($string){
        return is_string($string);
    }

    public static function isCleanString($string){
        return preg_match('/^[^\'"]+$/ui', $string);
    }

    public static function isDate($date){
        if (!preg_match('/^([0-9]{4})-((0?[1-9])|(1[0-2]))-((0?[1-9])|([1-2][0-9])|(3[01]))( [0-9]{2}:[0-9]{2}:[0-9]{2})?$/ui', $date, $matches))
            return false;
        return checkdate(intval($matches[2]), intval($matches[5]), intval($matches[0]));
    }

    public static function isBool($bool){
        return is_null($bool) || is_bool($bool) || preg_match('/^[0|1]{1}$/ui', $bool);
    }

    public static function isAddress($address){
        return empty($address) || preg_match('/^[^!<>?=+@{}_$%]*$/ui', $address);
    }

    public static function isUnsignedId($id){
        return self::isUnsignedInt($id);
    }
}
?>