@@ -238,4 +238,117 @@ function generateRandomStr($length = 8)
238
238
return substr ($ str , 0 , $ length );
239
239
}
240
240
241
+ /**
242
+ * 校验银行卡号是否输入有误 使用了Luhn算法校验
243
+ * @param int $no 银行卡号
244
+ * @return bool 返回数据校验结果
245
+ */
246
+ function luhm ($ no )
247
+ {
248
+ if (empty ($ no ) || !is_numeric ($ no ) || strlen ($ no ) < 16 )
249
+ {
250
+ return false ;
251
+ }
252
+ $ arr_no = str_split ($ no );
253
+ $ last_n = end ($ arr_no );
254
+ krsort ($ arr_no );
255
+ $ i = 1 ;
256
+ $ total = 0 ;
257
+ foreach ($ arr_no as $ n )
258
+ {
259
+ if ($ i %2 ==0 ) {
260
+ $ ix = $ n *2 ;
261
+ if ($ ix >= 10 ) {
262
+ $ nx = 1 + ($ ix % 10 );
263
+ $ total += $ nx ;
264
+ } else {
265
+ $ total += $ ix ;
266
+ }
267
+ } else {
268
+ $ total += $ n ;
269
+ }
270
+ $ i ++;
271
+ }
272
+ $ total -= $ last_n ;
273
+ $ total *= 9 ;
274
+ if ($ last_n == ($ total %10 )){
275
+ return true ;
276
+ }
277
+ return false ;
278
+ }
241
279
280
+
281
+ /**
282
+ * 身份证校验函数 start
283
+ */
284
+
285
+ function validation_filter_id_card ($ id_card )
286
+ {
287
+ if (strlen ($ id_card )==18 )
288
+ {
289
+ return idcard_checksum18 ($ id_card );
290
+ }
291
+ elseif ((strlen ($ id_card )==15 ))
292
+ {
293
+ $ id_card =idcard_15to18 ($ id_card );
294
+ return idcard_checksum18 ($ id_card );
295
+ }
296
+ else
297
+ {
298
+ return false ;
299
+ }
300
+ }
301
+
302
+ // 计算身份证校验码,根据国家标准GB 11643-1999
303
+ function idcard_verify_number ($ idcard_base )
304
+ {
305
+ if (strlen ($ idcard_base )!=17 ){
306
+ return false ;
307
+ }
308
+ //加权因子
309
+ $ factor =array (7 ,9 ,10 ,5 ,8 ,4 ,2 ,1 ,6 ,3 ,7 ,9 ,10 ,5 ,8 ,4 ,2 );
310
+ //校验码对应值
311
+ $ verify_number_list =array ('1 ' ,'0 ' ,'X ' ,'9 ' ,'8 ' ,'7 ' ,'6 ' ,'5 ' ,'4 ' ,'3 ' ,'2 ' );
312
+ $ checksum =0 ;
313
+ for ($ i =0 ;$ i <strlen ($ idcard_base );$ i ++) {
314
+ $ checksum += substr ($ idcard_base ,$ i ,1 ) * $ factor [$ i ];
315
+ }
316
+ $ mod =$ checksum % 11 ;
317
+ $ verify_number =$ verify_number_list [$ mod ];
318
+ return $ verify_number ;
319
+ }
320
+ // 将15位身份证升级到18位
321
+ function idcard_15to18 ($ idcard )
322
+ {
323
+ if (strlen ($ idcard )!=15 ){
324
+ return false ;
325
+ }else {
326
+ // 如果身份证顺序码是996 997 998 999,这些是为百岁以上老人的特殊编码
327
+ if (array_search (substr ($ idcard ,12 ,3 ),array ('996 ' ,'997 ' ,'998 ' ,'999 ' )) !== false ){
328
+ $ idcard =substr ($ idcard ,0 ,6 ).'18 ' .substr ($ idcard ,6 ,9 );
329
+ }else {
330
+ $ idcard =substr ($ idcard ,0 ,6 ).'19 ' .substr ($ idcard ,6 ,9 );
331
+ }
332
+ }
333
+ $ idcard =$ idcard .idcard_verify_number ($ idcard );
334
+ return $ idcard ;
335
+ }
336
+ // 18位身份证校验码有效性检查
337
+ function idcard_checksum18 ($ idcard )
338
+ {
339
+ if (strlen ($ idcard )!=18 ) {
340
+ return false ;
341
+ }
342
+ $ idcard_base =substr ($ idcard ,0 ,17 );
343
+ if (idcard_verify_number ($ idcard_base )!= strtoupper (substr ($ idcard ,17 ,1 ))) {
344
+ return false ;
345
+ } else {
346
+ return true ;
347
+ }
348
+ }
349
+
350
+ //调用方法如:
351
+ $ res = validation_filter_id_card ('44142319940113561X ' );
352
+ /**
353
+ * 身份证校验函数 end
354
+ */
0 commit comments