表单验证过滤类,PHP正则验证类

 

=

class post_get{
private $array; #交付表单检查测试字段和检查测量试验须求
# $type;  缺省为立异 或$type =”Into”;  
public function _post_get($array,$type=””){
  if(!$array) die(” Error!! “);
   foreach ($array as $v)
   {
   $KK = $this->ck_split($v);
   $KKarray = split(“_”, $KK);
   if(preg_match(“/Yes|No/is”,$KKarray[1]))
    {
    $escape_filter = ($KKarray[2])?
‘ck_’.$KKarray[2]:””;#亟待转义和过滤
    $data = ($escape_filter)?
$this->$escape_filter($_POST[$KKarray[0]]):$_POST[$KKarray[0]];
#输出合法数据
       if($data)
     {
     if(is_array($data))$data = implode(“,”,$data);
     $Into_data  .=”,’$data'”;
     $Into_Field.=”,`$KKarray[0]`”;
     $Updat_array[] =”`$KKarray[0]` = ‘$data'”;
     }
    }
    else $error .= ($KK)? “[“.$KK.”],”:””;
   }
   $_data=(!$type)?
$Updat_array:array(“$Into_Field”,”$Into_data”);
   return (!$error)? $_data:$this->ck_script($error); #
数组为合法表单验证结果,字符串为错误消息提醒
  }
public function ck_split($str){
  $strarray = explode(“,”,$str);
  foreach ($strarray as $i=> $var)
   if(0 ===$i)
   $name = trim($var);
  elseif(1===$i)
   {
   if(eregi(“No”,trim($var))&&!$_POST[$name])break; 
   #缺省忽略,不然…
   else $_type = (trim($var))? $name.’_’.trim($var):””; #检查测验后动作
   }
   elseif(2===$i)
   {
    $error = trim($var);
    if(!$_POST[$name])
    {
     $_error = $error;
     break;
    }
   }
   else # 检验部分
   {
    if(!trim($var))break;
    if(is_array($_POST[$name]))
    {
     foreach ($_POST[$name] as $_var)
      {
      $_error= ($this->ck_open($_var,trim($var)))?””:$error;
      if($_error)break;
      }
    }
    else
     {
     $_error=
($this->ck_open($_POST[$name],trim($var)))?””:$error;
     if($_error)break;
     }
   }
   return ($_error)? “””.$name.””,””.$_error.”””:$_type;
   }
# 窗口js提示
public function ck_script($str){
$str= trim($str,”,”);
  $str =  “
  <script>
  var str = new Array( $str );
  for (i=0; i < str.length; i++)
  {
  document.getElementById(‘J’+str[0]+”).innerHTML= str[1];
  }
  </script>
  “;
return $str;
}
# 类型推断
public function ck_detected($str){
  return (eregi(“^[a-zA-Z]*$”,$str))? ‘ck_’.$str:’ck_Length’;
}
# 函数调用
public function ck_open($string,$str){
  $functi = $this->ck_detected($str);
  return ($this->$functi($string,$str))? true:false;
}
#————————————-以下为检查评定函数  
# 长度
public function ck_Length($string,$str){
  $len = split(‘-‘,trim($str));
  return (strlen($string) > ($len[0]-1) && strlen($string) <
($len[1]+1))? true:false;
}
# 价格
public function ck_money($str){
  return preg_match(“/^(-|+)?d+(.d+)?$/”,$str);
}
# 邮件
public function ck_email($str){
  return
preg_match(“/^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$/”,
$str);
}
# 网址
public function ck_url($str){
  return
preg_match(“/^”,
$str);
}
# 数字型
public function ck_num($str){
  return is_numeric($str);
}
# 中文
public function ck_cn($str){
  return preg_match(“/^[x7f-xff]+$/”, $str);
}
# 字母
public function ck_en($str){
  return preg_match(“/^[A-Za-z]+$/”, $str);
}
# 数字字母混合
public function ck_numen($str){
  return preg_match(“/^([a-zA-Z0-9_-])+$/”,$str);
}
# 电话号码
public function ck_tel($str){
  return ereg(“^[+]?[0-9]+([xX-][0-9]+)*$”, $str);
}
# 敏感词
public function ck_key($str){
  Global $badkey;
  return (!preg_match(“/$badkey/i”,$str));
}
#—————————————————–输出
# 字符替换
public function ck_filter($str){
  $str=(is_array($str))? implode(“,”,$str):$str;
  $str=HTMLSpecialChars($str); //将特殊字元转成 HTML 格式。
  $str=nl2br($str); //将回车替换为<br>
  $str=str_replace(array(“ ”,'<? ‘),array(” “,'< ?’),$str);
//替换空格替换为
  return $str;
}
# MD5加密
public function ck_md($str){
  return  MD5($str);
}
# 转义
public function ck_escape($str){
  return  (is_array($str))? implode(“,”,$str):$str;
}
# 有法则注销(数字)
public function ck_cancel($str){
  return (!is_numeric($str))? $str:””;
}
# 无条件注销(数字)
public function ck_delete(){
  return null;
  }
}
**

<?php
/**
 *PHP正则验证类
 *Code by T.T.R
 *[url]] [url]]
 *QQ:252319874
 */
class regExp
{

## PHP

    static function strTrim($str)
    {
        return preg_replace(“/s/”,””,$str);
    }
 

“`

    static function userName($str,$type,$len)
    {
        $str=self::strTrim($str);
        if($len<strlen($str))
        {
            return false;
        }else{
            switch($type)
            {
                case “EN”://纯英文
                    if(preg_match(“/^[a-zA-Z]+$/”,$str))
                    {
                        return true;
                    }else{
                        return false;
                    }
                    break;
                case “ENNUM”://英文数字
                    if(preg_match(“/^[a-zA-Z0-9]+$/”,$str))
                    {
                        return true;
                    }else{
                        return false;
                    }
                    break;
                case “ALL”:    //允许的号子(|-_字母数字)
                    if(preg_match(“/^[|-_a-zA-Z0-9]+$/”,$str))
                    {
                        return true;
                    }else{
                        return false;
                    }
                    break;
            }
        }
    }
 

/*注解手提式有线电话机号*/

    static function
passWord($min,$max,$str)
    {
        $str=self::strTrim($str);
        if(strlen($str)>=$min && strlen($str)<=$max)
        {
            return true;
        }else{
            return false;
        }
    }

function is_mobile($str){

    static function Email($str)
    {
        $str=self::strTrim($str);
       
       
if(preg_match(“/^([a-z0-9_]|\-|\.)+@(([a-z0-9_]|\-)+\.){1,2}[a-z]{2,4}$/i”,$str))
        {
            return true;
        }else{
            return false;
        }
       
    }

return preg_math(‘/^1[34578]{1}\d{9}$/’,$str)?true:false;//13
347106430

    static function idCard($str)
    {
        $str=self::strTrim($str);
       
if(preg_match(“/^([0-9]{15}|[0-9]{17}[0-9a-z])$/i”,$str))
        {
            return true;
        }else{
            return false;
        }
    }
  

}

 

/*注脚邮箱号*/

    static function Phone($type,$str)
    {
        $str=self::strTrim($str);
        switch($type)
        {
            case “CHN”:
               
if(preg_match(“/^([0-9]{3}|0[0-9]{3})-[0-9]{7,8}$/”,$str))
                {
                    return true;
                }else{
                    return false;
                }
                break;
            case “INT”:
               
if(preg_match(“/^[0-9]{4}-([0-9]{3}|0[0-9]{3})-[0-9]{7,8}$/”,$str))
                {
                    return true;
                }else{
                    return false;
                }
                break;
        }
    }
}
$str=”008-010-2711204″;
if(regExp::Phone(“INT”,$str))
{
    echo “ok”;
}else{
    echo “no”;
}
?>
 

function isEmail($str)

{

return filter_var($str,FILTER_VALIDATE_EMAIL);

亚洲城ca88唯一备用,}

/*注解微能量信号*/

function isWeixinid($str)

{

return preg_match(‘/^[\w-]{4,20}/’, $str) !== 0;

}

/*注明密码*/

function verifypasswd($str, $min = 6, $max = 32, $minType = 2)

{

$len = strlen($str);

if ($len < $min || $len > $max) {

return false;

}

$score = 0;

if (preg_match(“/\d/”, $str))

$score++;

if (preg_match(“/[a-z]/”, $str))

$score++;

if (preg_match(“/[A-Z]/”, $str))

$score++;

if (preg_match(“/\W/”, $str))

$score++;

return $score >= $minType;

}

/*发生字符串*/

function getRandChar($length,$mode = 0)

{

$str = ”;

switch ($mode) {

case 3:

$strPol = “123456789”;

break;

default:

$strPol =
“ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz”;

break;

}

$max = strlen($strPol) – 1;

while ($length–) {

$str .= $strPol[mt_rand(0, $max)];

}

return $str;

}

“`

## Javascript

“`

“`

相关文章