我写的PHP数据验证类

<?php
/**
 * Name:        Validator
 * Author:      景银振
 * Description: A class for Validate all variables
 * DateTime:    2009-05-26 16:33:06
 * Version:     1.0.2
 * Date:        2009-09-28 13:03:02
 你可以随便使用这个类,但请在使用的时候著名作者以及版本信息,保留著作权中的署名权 ,违者追究法律责任
 */
class validator{
 # 是否是邮件格式
 public static function checkMail($str){
  return preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str);
 }
 public static function checkUrl($str){
  return preg_match("|^http://[_=&///?\.a-zA-Z0-9-]+$|i", $str);
 }
 # 是否全部由数字组成 (科学计数法匹配不成功)
 public static function checkNumber($str){
  return preg_match("/^[0-9]+$/", $str);
 }
 # 检查字符串 必须以字母或者下划线开头 只能包含字母 数字 下划线 上划线
 public static function checkStr($str){
  return preg_match("/^[a-zA-Z_]([_a-zA-Z0-9-])+$/i", $str);
 }
 # 滤掉JS脚本 
 public static function ClearJs($str){
  $text = trim($text);
  $text = stripslashes($text);
  $text = preg_replace('/<\?|\?>/','',$text);
  $text = preg_replace('/<script?.*\/script>/','',$text);
  $text = preg_replace('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset)[^><]*>/i','',$text);
  while(preg_match('/(<[^><]+)(lang|onfinish||onerror|||onfocus|onblur)[^><]+/i',$text,$mat)){
   $text=str_replace($mat[0],$mat[1],$text);
  }
  while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)){
   $text=str_replace($mat[0],$mat[1].$mat[3],$text);
  }
  return $text;
 }
 # 全部是中文
 public  static function checkCNAll($str){
    return (!eregi("[^\x80-\xff]",$str));
 }
 # 包含中文
 public  static function checkCN($str){
  return preg_match("/[\x80-\xff]./", $str);  
 }
 # 全是英文字母
 public static function checkEN($str){
  return preg_match("/^[A-Za-z]+$/", $str);
 }
 # 检查手机号码
 public  static function checkCell($str){
  if(ereg("^1[3|5][0-9]{9}$",$str)){
      return true;
  }
  return false;
 }
 # 匹配电话号码 (可以加区号)
 public  static function checkTel($str){
  return preg_match("/(1[35]\d{9}$|^0?((10)|(2\d{1})|([3-9]\d{2}))-)?[1-9]\d{6,7}(-\d{3,4})?$/", $str);
 }
 # 匹配邮编
 public  static function checkZip($str){
  if(strlen($str)!=6){
   return false;
  }
  if(substr($str,0,1)==0){
   return false;
  }
  return true;
 }
 # 匹配 QQ 号码
 public  static function checkQQ($str){
  if(ereg("^[1-9][0-9]{4,}$",$str)){
   return true;
  }
  return false;
 }
 # 匹配日期
 public static function checkDate($str){
  $dateArr = explode("-", $str);
  if (is_numeric($dateArr[0]) && is_numeric($dateArr[1]) && is_numeric($dateArr[2])) {
   if (($dateArr[0] >= 1000 && $timeArr[0] <= 10000) && ($dateArr[1] >= 0 && $dateArr[1] <= 12) && ($dateArr[2] >= 0 && $dateArr[2] <= 31))
    return true;
   else
    return false;
  }
  return false;
 }
 # 匹配时间
 public static function checkTime($str){
  $timeArr = explode(":", $str);
  if (is_numeric($timeArr[0]) && is_numeric($timeArr[1]) && is_numeric($timeArr[2])) {
   if (($timeArr[0] >= 0 && $timeArr[0] <= 23) && ($timeArr[1] >= 0 && $timeArr[1] <= 59) && ($timeArr[2] >= 0 && $timeArr[2] <= 59))
    return true;
   else
    return false;
  }
  return false;
 }
 # 匹配IP地址
 public static function checkIP($str){
  $exp = array();
  if($exp = explode('.', $str)) {
   foreach($exp as $val) {
    if($val > 255) {
     return FALSE;
     exit;
    }
   }
  }
  return preg_match("/^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/", $str);
 }
 # 匹配长度 ,第二个参数是最小长度 ,第三个参数 是最大长度
 public static function checkLen($str,$min=1,$max=0){
  $len=strlen($str);
  if($max===0){
   $flag=$len>$min?true:false;
   return $flag;
  }  
  $flag=($len>$min && $len<$max)?true:false;
  return $flag;  
 }
 # 自定义正则  进行匹配
 public static function checkReg($str,$reg){
     return preg_match($reg, $str);
 }
}
?>
 
请把附件的扩展名改为 .php

本文出自 “小景的博客,搬迁至 ht..” 博客,转载请与作者联系!

你可能感兴趣的:(Web,PHP,正则,休闲,数据验证)