php 验证函数(包括email,url,日期,手机号、ip ’等等)

  1. ?php    
  2. /**  
  3.  * [email protected]  zyy 
  4.  * 一些验证方法  
  5.  */    
  6. /**  
  7.  * 是否是手机号码  
  8.  *  
  9.  * @param string $phone 手机号码  
  10.  * @return boolean  
  11.  */    
  12. function is_phone($phone) {    
  13.     if (strlen ( $phone ) != 11 || ! preg_match ( '/^1[3|4|5|8][0-9]\d{4,8}$/'$phone )) {    
  14.         return false;    
  15.     } else {    
  16.         return true;    
  17.     }    
  18. }    
  19. /**  
  20.  * 验证字符串是否为数字,字母,中文和下划线构成  
  21.  * @param string $username  
  22.  * @return bool  
  23.  */    
  24. function is_check_string($str){    
  25.     if(preg_match('/^[\x{4e00}-\x{9fa5}\w_]+$/u',$str)){    
  26.         return true;    
  27.     }else{    
  28.         return false;    
  29.     }    
  30. }    
  31. /**  
  32.  * 是否为一个合法的email  
  33.  * @param sting $email  
  34.  * @return boolean  
  35.  */    
  36. function is_email($email){    
  37.     if (filter_var ($email, FILTER_VALIDATE_EMAIL )) {    
  38.         return true;    
  39.     } else {    
  40.         return false;    
  41.     }    
  42. }    
  43. /**  
  44.  * 是否为一个合法的url  
  45.  * @param string $url  
  46.  * @return boolean  
  47.  */    
  48. function is_url($url){    
  49.     if (filter_var ($url, FILTER_VALIDATE_URL )) {    
  50.         return true;    
  51.     } else {    
  52.         return false;    
  53.     }    
  54. }    
  55. /**  
  56.  * 是否为一个合法的ip地址  
  57.  * @param string $ip  
  58.  * @return boolean  
  59.  */    
  60. function is_ip($ip){    
  61.     if (ip2long($ip)) {    
  62.         return true;    
  63.     } else {    
  64.         return false;    
  65.     }    
  66. }    
  67. /**  
  68.  * 是否为整数  
  69.  * @param int $number  
  70.  * @return boolean  
  71.  */    
  72. function is_number($number){    
  73.     if(preg_match('/^[-\+]?\d+$/',$number)){    
  74.         return true;    
  75.     }else{    
  76.         return false;    
  77.     }    
  78. }    
  79. /**  
  80.  * 是否为正整数  
  81.  * @param int $number  
  82.  * @return boolean  
  83.  */    
  84. function is_positive_number($number){    
  85.     if(ctype_digit ($number)){    
  86.         return true;    
  87.     }else{    
  88.         return false;    
  89.     }    
  90. }    
  91. /**  
  92.  * 是否为小数  
  93.  * @param float $number  
  94.  * @return boolean  
  95.  */    
  96. function is_decimal($number){    
  97.     if(preg_match('/^[-\+]?\d+(\.\d+)?$/',$number)){    
  98.         return true;    
  99.     }else{    
  100.         return false;    
  101.     }    
  102. }    
  103. /**  
  104.  * 是否为正小数  
  105.  * @param float $number  
  106.  * @return boolean  
  107.  */    
  108. function is_positive_decimal($number){    
  109.     if(preg_match('/^\d+(\.\d+)?$/',$number)){    
  110.         return true;    
  111.     }else{    
  112.         return false;    
  113.     }    
  114. }    
  115. /**  
  116.  * 是否为英文  
  117.  * @param string $str  
  118.  * @return boolean  
  119.  */    
  120. function is_english($str){    
  121.     if(ctype_alpha($str))    
  122.         return true;    
  123.     else    
  124.         return false;    
  125. }    
  126. /**  
  127.  * 是否为中文  
  128.  * @param string $str  
  129.  * @return boolean  
  130.  */    
  131. function is_chinese($str){    
  132.     if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$str))    
  133.         return true;    
  134.     else     
  135.         return false;    
  136. }    
  137. /**  
  138.  * 判断是否为图片  
  139.  * @param string $file  图片文件路径  
  140.  * @return boolean  
  141.  */    
  142. function is_image($file){    
  143.     if(file_exists($file)&&getimagesize($file===false)){    
  144.         return false;    
  145.     }else{    
  146.         return true;    
  147.     }    
  148. }    
  149. /**  
  150.  * 是否为合法的身份证(支持15位和18位)  
  151.  * @param string $card  
  152.  * @return boolean  
  153.  */    
  154. function is_card($card){    
  155.     if(preg_match('/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/',$card)||preg_match('/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/',$card))    
  156.         return true;    
  157.     else     
  158.         return false;    
  159. }    
  160. /**  
  161.  * 验证日期格式是否正确  
  162.  * @param string $date  
  163.  * @param string $format  
  164.  * @return boolean  
  165.  */    
  166. function is_date($date,$format='Y-m-d'){    
  167.     $t=date_parse_from_format($format,$date);    
  168.     if(empty($t['errors'])){    
  169.         return true;    
  170.     }else{    
  171.         return false;    
  172.     }    
  173. }    
  174. ?>    

你可能感兴趣的:(php 验证函数(包括email,url,日期,手机号、ip ’等等))