首页 > 编程技术 > php

常用的正则表达试

发布时间:2016-11-25 15:58

 
 public function funcStr($str,$num1='',$num2='') //字符正则表达试
 {
  if($num1!='' and $num2!=''){
   return (preg_match("/^[a-zA-Z]{".$num1.",".$num2."}$/",$str))?true:false;
  }else{
   return (preg_match("/^[a-zA-Z]/",$str))?true:false;
  }  
 }
 
 public function funcNum($str,$num1='',$num2='')//数字正则表达试
 {
  if($num1!='' and $num2!=''){
   return (preg_match("/^[0-9]{".$num1.",".$num2."}$/",$str))?true:false;
  }else{
   return (preg_match("/^[0-9]/",$str))?true:false;
  }
 }
 
 public function funcCard($str)//
 {
  return (preg_match('/(^([d]{15}|[d]{18}|[d]{17}x)$)/',$str))?true:false;
 }
 
 public function funcEmail($str)//邮箱正则表达式
 {
  return (preg_match('/^[_.0-9a-z-A-Z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,4}$/',$str))?true:false;
 }
 
 public function funcPhone($str)//电话号码正则表达试
 {
  return (preg_match("/^(((d{3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9]d{6,8}$/",$str))?true:false;
 }    
 
 public function funcMtel($str)//手机号码正则表达试
 {
  return (preg_match("/(?:13d{1}|15[03689])d{8}$/",$str))?true:false;
 } 
 
 public function funcZip($str)//邮编正则表达试
 {
  return (preg_match("/^[0-9]d{5}$/",$str))?true:false;
 } 
 
 public function funcUrl($str)//url正则表达试
 {
  return (preg_match("/^http://[A-Za-z0-9]+.[A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^<>""])*$/",$str))?true:false;
 }  

class Trees{
 private $_keyId = 'Id';
 private $_keyName = 'Name';
 private $_keyFid = 'Fid';
 
 function __construct($keyId='',$keyName='',$keyFid='')
 {
  if($keyId==""){$this->_keyId = $this->_keyId;}else{$this->_keyId = $keyId;}
  if($keyName==""){$this->_keyName = $this->_keyName;}else{$this->_keyName = $keyName;}
  if($keyFid==""){$this->_keyFid = $this->_keyFid;}else{$this->_keyFid = $keyFid;}
 }
 
 public function treeListAll($fid,$step=0,&$fromArray,&$resultArray)
 {
  $step++;
  foreach ($fromArray as $k=>$v){
   if($v[$this->_keyFid] == $fid){
    $newArray[] = $v;
   }
  }
  if(isset($newArray)){
   foreach ($newArray as $k=>$v){
    $this->treeListAll($v[$this->_keyId],$step,$fromArray,$resultArray);
    $v['Step'] = $step;
    $resultArray[] = $v;
   }
  }
 }
 
 public function getTreeListAll($fid=0,$step=0,&$fromArray,&$resultArray)
 {
  $step++;
  foreach ($fromArray as $k=>$v){
   if($v[$this->_keyFid] == $fid){
    $newArray[] = $v;
   }
  }
  if(isset($newArray)){
   foreach ($newArray as $k=>$v){
    $this->getTreeListAll($v[$this->_keyId],$step,$fromArray,$resultArray);
    $v['Step'] = $step;
    $resultArray[] = $v;
   }
  }  
 }
 
 public function getTreeList($id = 0,&$fromArray)
 {
  $resultArray = array();
  $this->getTreeListAll($id,0,$fromArray,$resultArray);
  $resultArray = array_reverse($resultArray);
  return $resultArray;
 }
 
 public function getTreeArray($id = 0,&$fromArray)
 {
  $result_one_array = $this->getTreeList($id,$fromArray);
  foreach ($result_one_array as $k=>$v){
   $result_two_array[] = array($v[$this->_keyId]);
  }
  if(isset($result_two_array)){
  for ($i=0;$i<count($result_two_array);$i++){
   for ($j=0;$j<count($result_two_array[$i]);$j++){
    $result[] = $result_two_array[$i][$j];
   }
  }
  }else{
   $result = array();
  }
  return $result;
 }

 public function treeListAllTop($fid,$step=0,&$fromArray,&$resultArray)
 {
  $step++;
  foreach ($fromArray as $k=>$v){
   if($v[$this->_keyId] == $fid){
    $newArray[] = $v;
   }
  }
  if(isset($newArray)){
   foreach ($newArray as $k=>$v){
    $this->treeListAllTop($v[$this->_keyFid],$step,$fromArray,$resultArray);
    $v['Step'] = $step;
    $resultArray[] = $v;
   }
  }
 }
 
 public function getTreeListAllTop($fid=0,$step=0,&$fromArray,&$resultArray)
 {
  $step++;
  foreach ($fromArray as $k=>$v){
   if($v[$this->_keyId] == $fid){
    $newArray[] = $v;
   }
  }
  if(isset($newArray)){
   foreach ($newArray as $k=>$v){
    $this->getTreeListAllTop($v[$this->_keyFid],$step,$fromArray,$resultArray);
    $v['Step'] = $step;
    $resultArray[] = $v;
   }
  }  
 }
 
 public function getTreeListTop($id = 0,&$fromArray)
 {
  $resultArray = array();
  $this->getTreeListAllTop($id,0,$fromArray,$resultArray);
  $resultArray = array_reverse($resultArray);
  return $resultArray;
 }
 
 public function getTreeArrayTop($id = 0,&$fromArray)
 {
  $result_one_array = $this->getTreeListTop($id,$fromArray);
  foreach ($result_one_array as $k=>$v){
   $result_two_array[] = array($v[$this->_keyFid]);
  }
  if(isset($result_two_array)){
  for ($i=0;$i<count($result_two_array);$i++){
   for ($j=0;$j<count($result_two_array[$i]);$j++){
    $result[] = $result_two_array[$i][$j];
   }
  }
  }else{
   $result = array();
  }
  return $result;
 }
  
 public function makeOptionString($sourcArray,$firstHint="顶级分类",$selectId=array('-1'),$type=0)
 {
  if($type==0){
   if($firstHint != ""){
    $str = '<option value="0">'.$firstHint.'</option>';
   }else{
    $str = ''; 
   }
   foreach ($sourcArray as $value){
    $level="";
    for($i=1;$i<$value['Step'];$i++){
     $level =$level."----|";
    }
    $selectStr = "";
    if(in_array($value[$this->_keyId],$selectId)){
     $selectStr = "selected";
    }else{
     
    }
    $str.='<option value="'.$value[$this->_keyId].'" '.$selectStr.'>|'.$level.$value[$this->_keyName]."</option>";
    $level="";
   }
  }else{
   $flagStep =-1;
   $str = '<option value="0">'.$firstHint.'</option>';
   foreach ($sourcArray as $value){
    $level="";
    for($i=1;$i<$value['Step'];$i++){
     $level =$level."----|";
    }
    $selectStr = "";
    if($type==$value[$this->_keyId]){
     $flagStep = $value['Step'];
    }else{
     if($flagStep != -1 && $value['Step']>$flagStep){
     
     }else{
      if($flagStep != -1 && $value['Step']<=$flagStep){
       $flagStep = -1;
      }
      if($value[$this->_keyId] == $selectId){
       $selectStr = "selected";
      }
      $str.='<option value="'.$value[$this->_keyId].'" '.$selectStr.'>|'.$level.$value[$this->_keyName]."</option>";
     }
    }
    
    $level="";
   }
  }
  return $str;
 }
}

class Client{
 
 public function __construct()
 {
  /**/
 }
 
 /**
  * 获取浏览器客户端
  *
  * @return string
  */
 public function browser()
 {
  $info = $_SERVER['HTTP_USER_AGENT'];
  if(strstr($info,'MSIE 6.0') != false){
   return 'IE6';
  }elseif (strstr($info,'MSIE 7.0') != false){
   return 'IE7';
  }elseif (strstr($info,'Firefox') != false){
   return 'Firefox';
  }elseif (strstr($info,'Chrome') != false){
   return 'Chrome';
  }elseif (strstr($info,'Safari') != false){
   return 'Safari';         
  }else{
   return 'unknow';
  }
  }
 
     /**
      * 获取操作系统
      * @return string
      */
  public function getOS ()
     {
      $agent = $_SERVER['HTTP_USER_AGENT'];
      $os = false;
      if (eregi('win', $agent) && strpos($agent, '95')){
          $os = 'Windows 95';
      }
      else if (eregi('win 9x', $agent) && strpos($agent, '4.90')){
          $os = 'Windows ME';
      }
      else if (eregi('win', $agent) && ereg('98', $agent)){
         $os = 'Windows 98';
      }
      else if (eregi('win', $agent) && eregi('nt 5.1', $agent)){
          $os = 'Windows XP';
      }
      else if (eregi('win', $agent) && eregi('nt 5', $agent)){
          $os = 'Windows 2000';
      }
      else if (eregi('win', $agent) && eregi('nt', $agent)){
          $os = 'Windows NT';
      }
      else if (eregi('win', $agent) && ereg('32', $agent)){
          $os = 'Windows 32';
      }
      else if (eregi('linux', $agent)){
          $os = 'Linux';
      }
      else if (eregi('unix', $agent)){
          $os = 'Unix';
      }
      else if (eregi('sun', $agent) && eregi('os', $agent)){
          $os = 'SunOS';
      }
      else if (eregi('ibm', $agent) && eregi('os', $agent)){
          $os = 'IBM OS/2';
      }
      else if (eregi('Mac', $agent) && eregi('PC', $agent)){
          $os = 'Macintosh';
      }
      else if (eregi('PowerPC', $agent)){
          $os = 'PowerPC';
      }
      else if (eregi('AIX', $agent)){
          $os = 'AIX';
      }
      else if (eregi('HPUX', $agent)){
          $os = 'HPUX';
      }
      else if (eregi('NetBSD', $agent)){
          $os = 'NetBSD';
      }
      else if (eregi('BSD', $agent)){
          $os = 'BSD';
      }
      else if (ereg('OSF1', $agent)){
          $os = 'OSF1';
      }
      else if (ereg('IRIX', $agent)){
          $os = 'IRIX';
      }
      else if (eregi('FreeBSD', $agent)){
          $os = 'FreeBSD';
      }
      else if (eregi('teleport', $agent)){
          $os = 'teleport';
      }
      else if (eregi('flashget', $agent)){
          $os = 'flashget';
      }
      else if (eregi('webzip', $agent)){
          $os = 'webzip';
      }
      else if (eregi('offline', $agent)){
          $os = 'offline';
      }
      else {
          $os = 'Unknown';
      }
     return $os;
     } 

     /**
      * 获取IP地址
      * @return string
      */
 public function getIp()
 {
  if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")){
   $ip = getenv("HTTP_CLIENT_IP");
        }elseif (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        }elseif (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")){
            $ip = getenv("REMOTE_ADDR");
        }elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))  {
            $ip = $_SERVER['REMOTE_ADDR'];
        }else{
            $ip = "unknown";
        }
        return($ip);
 }    

        Header("Content-type: image/PNG"); 
        srand((double)microtime()*1000000);
        $im = imagecreate(58,25);
        $black = ImageColorAllocate($im, 0,0,0);
        $white = ImageColorAllocate($im, 255,255,255);
        $gray = ImageColorAllocate($im, 200,200,200);
        imagefill($im,68,30,$gray);

   //将四位整数验证码绘入图片
        imagestring($im, 5, 10, 8, $HTTP_GET_VARS['authnum'], $black);

        for($i=0;$i<200;$i++)   //加入干扰象素
        {
                imagesetpixel($im, rand()%70 , rand()%30 , $black);
        }

        ImagePNG($im);
        ImageDestroy($im);

 

 session_start();
 $img_width=70;    //先定义图片的长、宽
 $img_height=28;
    $num_array=range(0,9);
 $small_char_array=range('a','z');
 $big_char_array=range('A','Z');
 $myarray=array_merge($num_array,$small_char_array,$big_char_array);
 $mystring=$myarray[mt_rand(0,61)].$myarray[mt_rand(0,61)].$myarray[mt_rand(0,61)].$myarray[mt_rand(0,61)];
    //exit($mystring);
 //unset($_SESSION['shuzi']);
    $_SESSION['shuzi'] = $mystring;
    $aimg = imagecreate($img_width,$img_height);    //生成图片
    imagecolorallocate($aimg, 255,255,255);            //图片底色,ImageColorAllocate第1次定义颜色PHP就认为是底色了
    $black = imagecolorallocate($aimg,200,200,200);        //定义需要的黑色
    imagerectangle($aimg,0,0,$img_width-1,$img_height-1,$black);//先成一黑色的矩形把图片包围

    //下面该生成雪花背景了,其实就是在图片上生成一些符号
    for ($i=1; $i<=100; $i++) {    //先用100个做测试
        imagestring($aimg,1,mt_rand(1,$img_width),mt_rand(1,$img_height),"*",imageColorAllocate($aimg,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255)));
        //其实也不是雪花,就是生成*号而已。为了使它们看起来"杂乱无章、5颜6色",就得在1个1个生成它们的时候,让它们的位置、颜色,甚至大小都用随机数,rand()或mt_rand都可以完成。
    }

    //上面生成了背景,现在就该把已经生成的随机数放上来了。道理和上面差不多,随机数1个1个地放,同时让他们的位置、大小、颜色都用成随机数~~
    //为了区别于背景,这里的颜色不超过200,上面的不小于200

        imagestring($aimg,5,mt_rand(5,35),mt_rand(5,$img_height/2),$mystring,imageColorAllocate($aimg,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200)));
 
    Header("Content-type: image/png");    //告诉浏览器,下面的数据是图片,而不要按文字显示
    ImagePng($aimg);                    //生成png格式。。。嘿嘿效果蛮像回事的嘛。。。
    ImageDestroy($aimg);

 

标签:[!--infotagslink--]

您可能感兴趣的文章: