php银行卡校验

WBOY
Release: 2016-06-23 13:25:53
Original
1093 people have browsed it

前言
银行金卡,维萨和万事达、银联品牌,如果是贷记卡或准贷记卡,一定为16位卡号。而借记卡可以16-19位不等。
美国运通卡则不论金卡或是白金卡、普通卡,都是15位卡号。
16-19 位卡号校验位采用 Luhm 校验方法计算

银行卡的校验方式

Luhm校验

  1. 将未带校验位的 15 位卡号从右依次编号 1 到 15,位于奇数位号上的数字乘以 2
  2. 将奇位乘积的个十位全部相加,再加上所有偶数位上的数字
  3. 将加法和加上校验位能被 10 整除。
<?php/*  16-19 位卡号校验位采用 Luhm 校验方法计算:    1,将未带校验位的 15 位卡号从右依次编号 1 到 15,位于奇数位号上的数字乘以 2    2,将奇位乘积的个十位全部相加,再加上所有偶数位上的数字    3,将加法和加上校验位能被 10 整除。*/function luhm($s) {    $n = 0;    for ($i = strlen($s); $i >= 1; $i--) {        $index=$i-1;        //偶数位        if ($i % 2==0) {            $n += $s{$index};        } else {//奇数位            $t = $s{$index} * 2;            if ($t > 9) {                $t = (int)($t/10)+ $t%10;            }            $n += $t;        }    }    return ($n % 10) == 0;}$r = luhm('6225881414207430');var_dump($r);?>
Copy after login

准确率怎么样,附文一片
银行卡校验规则(Luhn算法)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!