@@ -49,11 +49,79 @@ static public function intRange($data,$num1,$num2){
4949 return filter_var ($ data ,FILTER_VALIDATE_INT ,array ('options ' =>array ('min_range ' =>$ num1 ,'max_range ' =>$ num2 )));
5050 }
5151
52+ // 验证身份证 http://baike.baidu.com/view/5112521.htm
53+ static public function isIDCard ($ IDCard ) {
54+ if (strlen ($ IDCard ) == 18 ) {
55+ return self ::check18IDCard ($ IDCard );
56+ } elseif ((strlen ($ IDCard ) == 15 )) {
57+ $ IDCard = self ::convertIDCard15to18 ($ IDCard );
58+ return self ::check18IDCard ($ IDCard );
59+ } else {
60+ return false ;
61+ }
62+ }
63+
64+ //计算身份证的最后一位验证码,根据国家标准GB 11643-1999
65+ static public function calcIDCardCode ($ IDCardBody ) {
66+ if (strlen ($ IDCardBody ) != 17 ) {
67+ return false ;
68+ }
69+
70+ //加权因子
71+ $ factor = array (7 , 9 , 10 , 5 , 8 , 4 , 2 , 1 , 6 , 3 , 7 , 9 , 10 , 5 , 8 , 4 , 2 );
72+ //校验码对应值
73+ $ code = array ('1 ' , '0 ' , 'X ' , '9 ' , '8 ' , '7 ' , '6 ' , '5 ' , '4 ' , '3 ' , '2 ' );
74+ $ checksum = 0 ;
75+
76+ for ($ i = 0 ; $ i < strlen ($ IDCardBody ); $ i ++) {
77+ $ checksum += substr ($ IDCardBody , $ i , 1 ) * $ factor [$ i ];
78+ }
79+
80+ return $ code [$ checksum % 11 ];
81+ }
82+
83+ // 将15位身份证升级到18位
84+ static public function convertIDCard15to18 ($ IDCard ) {
85+ if (strlen ($ IDCard ) != 15 ) {
86+ return false ;
87+ } else {
88+ // 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
89+ if (array_search (substr ($ IDCard , 12 , 3 ), array ('996 ' , '997 ' , '998 ' , '999 ' )) !== false ) {
90+ $ IDCard = substr ($ IDCard , 0 , 6 ) . '18 ' . substr ($ IDCard , 6 , 9 );
91+ } else {
92+ $ IDCard = substr ($ IDCard , 0 , 6 ) . '19 ' . substr ($ IDCard , 6 , 9 );
93+ }
94+ }
95+ $ IDCard = $ IDCard . self ::calcIDCardCode ($ IDCard );
96+ return $ IDCard ;
97+ }
98+
99+ // 18位身份证校验码有效性检查
100+ static public function check18IDCard ($ IDCard ) {
101+ if (strlen ($ IDCard ) != 18 ) {
102+ return false ;
103+ }
104+
105+ $ IDCardBody = substr ($ IDCard , 0 , 17 ); //身份证主体
106+ $ IDCardCode = strtoupper (substr ($ IDCard , 17 , 1 )); //身份证最后一位的验证码
107+
108+ if (self ::calcIDCardCode ($ IDCardBody ) != $ IDCardCode ) {
109+ return false ;
110+ } else {
111+ return true ;
112+ }
113+ }
114+
115+ //是否汉字
116+ static public function isChineseString ($ str ){
117+ return preg_match ("/^[ \x7f- \xff]+$/ " ,$ str );
118+ }
119+
52120 /*
53121 static public function isEmail($data) {
54122 if (!preg_match('/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/',$data)) return true;
55123 return false;
56124 }
57125 */
58- }
126+ }
59127?>
0 commit comments