1 <?php 2 /** 3 * @description: 正则表达式匹配 4 */ 5 class Regex { 6 /** 7 * @手机号 8 */ 9 public static function Phone($subject) { 10 $pattern='/^(0|86|17951)?(13[0-9]|15[012356789]|1[78][0-9]|14[57])[0-9]{8}$/'; 11 return Regex::PublicMethod($pattern, $subject); 12 } 13 /** 14 * @数字 15 */ 16 public static function Number($subject) { 17 $pattern='/^[0-9]+$/'; 18 return Regex::PublicMethod($pattern, $subject); 19 } 20 /** 21 * @年份 格式:yyyy 22 */ 23 public static function Year($subject) { 24 $pattern='/^(\d{4})$/'; 25 return Regex::PublicMethod($pattern, $subject); 26 } 27 /** 28 * @月份 格式:mm 29 */ 30 public static function Month($subject) { 31 $pattern='/^0?([1-9])$|^(1[0-2])$/'; 32 return Regex::PublicMethod($pattern, $subject); 33 } 34 /** 35 * @日期 格式:yyyy-mm-dd 36 */ 37 public static function Day($subject) { 38 $pattern='/^(\d{4})-(0?\d{1}|1[0-2])-(0?\d{1}|[12]\d{1}|3[01])$/'; 39 return Regex::PublicMethod($pattern, $subject); 40 } 41 /** 42 * @日期时间 格式:yyyy-mm-dd hh:ii:ss 43 */ 44 public static function DateTime($subject) { 45 $pattern='/^(\d{4})-(0?\d{1}|1[0-2])-(0?\d{1}|[12]\d{1}|3[01])\s(0\d{1}|1\d{1}|2[0-3]):[0-5]\d{1}:([0-5]\d{1})$/'; 46 return Regex::PublicMethod($pattern, $subject); 47 } 48 /** 49 * @邮箱 50 */ 51 public static function Email($subject) { 52 $pattern='/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/'; 53 return Regex::PublicMethod($pattern, $subject); 54 } 55 /** 56 * @邮编 57 */ 58 public static function Postcode($subject) { 59 $pattern='/[1-9]\d{5}(?!\d)/'; 60 return Regex::PublicMethod($pattern, $subject); 61 } 62 /** 63 * @有效图片地址 64 */ 65 public static function Photo($subject) { 66 $pattern='/\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/'; 67 return Regex::PublicMethod($pattern, $subject); 68 } 69 /** 70 * @URL地址 71 */ 72 public static function UrlAddress($subject) { 73 $pattern='/\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/'; 74 return Regex::PublicMethod($pattern, $subject); 75 } 76 /** 77 * @有效HTTP地址 78 */ 79 public static function EffectiveHttp($subject) { 80 $pattern='/\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))/'; 81 return Regex::PublicMethod($pattern, $subject); 82 } 83 /** 84 * @身份证 85 */ 86 public static function Identity($subject) { 87 $pattern='/(^\d{15}$)|(^\d{17}([0-9]|X)$)/'; 88 return Regex::PublicMethod($pattern, $subject); 89 } 90 /** 91 * @IPv4 92 */ 93 public static function Ipv4($subject) { 94 $pattern='/^(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))$/'; 95 return Regex::PublicMethod($pattern, $subject); 96 } 97 /** 98 * @IPv6 99 */ 100 public static function Ipv6($subject) { 101 $pattern='/^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$/'; 102 return Regex::PublicMethod($pattern, $subject); 103 } 104 /** 105 * @匹配正则公共方法 106 */ 107 public static function PublicMethod($pattern, $subject){ 108 if(preg_match($pattern, $subject)){ 109 return true; 110 } 111 return false; 112 } 113 }