现在大部分的主要信用卡也是用 LUHN 算法来实现, 包括 Visa, Master Card, American Express以及 Discover 等。
LUHN 算法只可以检测信用卡号码的合法性,而不会检测信用卡的其他信息,包括是否过期。
使用方法为:
1. 检测信用卡号码是否为 16 位
2. 将信用卡号码串切割成 16 个数字
3. 将切割的数字由左至右起,每逢单数位置的数字乘以 2,如果结果大于 10,将结果减 9
4. 将所有乘以 2 位置的数字的结果(包括大于 10,将结果减 9的)相加得到一个新数值
5. 将上面求得的数值求出 10 的余数,如果余数是 0 表示信用卡号码正确,否则便是错误。
1<?php
2
3
9
10
11functionluhn_checker($card_num){
12//将非数字的字符串移除
13$card_num=preg_replace("/D|s/","",$card_num);
14
15$sum=0;
16for($i=0;$i<strlen($card_num);$i++){
17$digit=substr($card_num,$i,1);
18if(($i%2)==0){
19//将单数位置的数值乘以2
20$digit=$digit*2;
21}
22
23if($digit>9)$digit=$digit-9;
24$sum+=$digit;
25}
26
27if(($sum%10)==0&&strlen($card_num)==16){
28returnTRUE;
29}else{
30returnFALSE;
31}
32}
33
34
41?>
MasterCard信用卡测试卡号-creditcard-1
5105105105105100
5111111111111118
5454545454545454
5500000000000004
5555555555551111
5555555555554444
VISA信用卡测试卡号-creditcard-2
4590613013277775
4111111111111111
4226146578860117
4067425543164587
4007000000027
4222222222222
4012888888881881
AmericanExpress信用卡测试卡号-creditcard-3
378282246310005
3111111111111117
343434343434343
370000000000002
340000000000009
371449635398431
378734493671000
Diner’sClub信用卡测试卡号-creditcard-4
38520000023237
30000000000004
30569309025904
Discover信用卡测试卡号-creditcard-5
6011111111111117
6011000000000004
6011000990139424
6011601160116611
6111111111111116
JCB信用卡测试卡号-creditcard-6
3530111333300000
3088000000000009
3566002020360505
Test Credit Card Account Numbers-creditcard-
While testing, use only the credit card numbers listed here. Othernumbers produce an error.
Expiration Datemust be avalid date in the future (usethemmyyformat).
Test Credit Card Account Numbers
| Credit Card Type | Credit Card Number |
| American Express | 378282246310005 |
| American Express | 371449635398431 |
| American Express Corporate | 378734493671000 |
| Australian BankCard | 5610591081018250 |
| Diners Club | 30569309025904 |
| Diners Club | 38520000023237 |
| Discover | 6011111111111117 |
| Discover | 6011000990139424 |
| JCB | 3530111333300000 |
| JCB | 3566002020360505 |
| MasterCard | 5555555555554444 |
| MasterCard | 5105105105105100 |
| Visa | 4111111111111111 |
| Visa | 4012888888881881 |
| Visa | 4222222222222Note:Eventhough this number has a different character count than the othertest numbers, it is the correct and functional number. |
| Processor-specific Cards | |
| Dankort (PBS) | 76009244561 |
| Dankort (PBS) | 5019717010103742 |
| Switch/Solo (Paymentech) | 6331101999990016 |
爱华网



