php 验证信用卡卡号的解决办法

内容摘要
这篇文章主要为大家详细介绍了php 验证信用卡卡号的简单示例,具有一定的参考价值,可以用来参考一下。

对php验证信用卡卡号代码对此感兴趣的朋友,看看idc笔记做的技术笔记!php
文章正文

这篇文章主要为大家详细介绍了php 验证信用卡卡号的简单示例,具有一定的参考价值,可以用来参考一下。

对php验证信用卡卡号代码对此感兴趣的朋友,看看idc笔记做的技术笔记!php验证信用卡卡号代码,可以用来验证普通信用卡,visa,master等信用卡,采用了Luhn算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<code class="php">
/**
 * php验证信用卡卡号代码
 *
 * @param
 * @arrange 512-笔记网: www.512PiC.com
 **/
// This function will take a credit card number and check to make sure it
// contains the right amount of digits and uses the Luhn Algorithm to
// weed out made up numbers
function validateCreditcard_number($credit_card_number)
{
    // Get the first digit
    $firstnumber = substr($credit_card_number, 0, 1);
    // Make sure it is the correct amount of digits. Account for dashes being present.
    switch ($firstnumber)
    {
        case 3:
            if (!preg_match('/^3\d{3}[ \-]?\d{6}[ \-]?\d{5}$/', $credit_card_number))
            {
                return 'This is not a valid American Express card number';
            }
            break;
        case 4:
            if (!preg_match('/^4\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $credit_card_number))
            {
                return 'This is not a valid Visa card number';
            }
            break;
        case 5:
            if (!preg_match('/^5\d{3}[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $credit_card_number))
            {
                return 'This is not a valid MasterCard card number';
            }
            break;
        case 6:
            if (!preg_match('/^6011[ \-]?\d{4}[ \-]?\d{4}[ \-]?\d{4}$/', $credit_card_number))
            {
                return 'This is not a valid Discover card number';
            }
            break;
        default:
            return 'This is not a valid credit card number';
    }
    // Here's where we use the Luhn Algorithm
    $credit_card_number = str_replace('-', '', $credit_card_number);
    $map = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
                0, 2, 4, 6, 8, 1, 3, 5, 7, 9);
    $sum = 0;
    $last = strlen($credit_card_number) - 1;
    for ($i = 0; $i <= $last; $i++)
    {
        $sum += $map[$credit_card_number[$last - $i] + ($i & 1) * 10];
    }
    if ($sum % 10 != 0)
    {
        return 'This is not a valid credit card number';
    }
    // If we made it this far the credit card number is in a valid format
    return 'This is a valid credit card number';
}
echo validateCreditcard_number('4111-1111-1111-1111'); // This is a valid credit card number
echo validateCreditcard_number('4111-1111-1111-1112'); // This is not a valid credit card number
echo validateCreditcard_number('5558-545f-1234');      // This is not a valid MasterCard card number
echo validateCreditcard_number('9876-5432-1012-3456'); // This is not a valid credit card number
/***   来自php教程(www.idcnote.com)   ***/</code>

注:关于php 验证信用卡卡号的简单示例的内容就先介绍到这里,更多相关文章的可以留意

代码注释

作者:喵哥笔记

IDC笔记

学的不仅是技术,更是梦想!