首页 > 后端开发 > php教程 > php信誉卡卡号验证函数

php信誉卡卡号验证函数

WBOY
发布: 2016-06-13 12:37:09
原创
845 人浏览过

php信用卡卡号验证函数

介绍一个php信用卡卡号验证函数,可以验证一个卡号是否是信用卡。

 

<span class="kwd">function</span><span class="pln"> validateCard </span><span class="pun">(</span><span class="pln">$cardnumber</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    $cardnumber </span><span class="pun">=</span><span class="pln"> preg_replace </span><span class="pun">(</span><span class="str">"/\D|\s/"</span><span class="pun">,</span><span class="pln"> </span><span class="str">""</span><span class="pun">,</span><span class="pln"> $cardnumber</span><span class="pun">);</span><span class="pln">
    $cardlength </span><span class="pun">=</span><span class="pln"> strlen</span><span class="pun">(</span><span class="pln">$cardnumber</span><span class="pun">);</span><span class="pln">
    </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$cardlength </span><span class="pun">!=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
        $parity </span><span class="pun">=</span><span class="pln"> $cardlength </span><span class="pun">%</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">
        $sum </span><span class="pun">=</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
        </span><span class="kwd">for</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln"> $i</span><span class="pun"><</span><span class="pln">$cardlength</span><span class="pun">;</span><span class="pln"> $i</span><span class="pun">&#43;&#43;)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
            $digit </span><span class="pun">=</span><span class="pln"> $cardnumber</span><span class="pun">[</span><span class="pln">$i</span><span class="pun">];</span><span class="pln">
            </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$i </span><span class="pun">%</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> $parity</span><span class="pun">)</span><span class="pln"> $digit </span><span class="pun">=</span><span class="pln"> $digit </span><span class="pun">*</span><span class="pln"> </span><span class="lit">2</span><span class="pun">;</span><span class="pln">
            </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$digit </span><span class="pun">></span><span class="pln"> </span><span class="lit">9</span><span class="pun">)</span><span class="pln"> $digit </span><span class="pun">=</span><span class="pln"> $digit </span><span class="pun">-</span><span class="pln"> </span><span class="lit">9</span><span class="pun">;</span><span class="pln">
            $sum </span><span class="pun">=</span><span class="pln"> $sum </span><span class="pun">&#43;</span><span class="pln"> $digit</span><span class="pun">;</span><span class="pln">
        </span><span class="pun">}</span><span class="pln">
        $valid </span><span class="pun">=</span><span class="pln"> </span><span class="pun">(</span><span class="pln">$sum </span><span class="pun">%</span><span class="pln"> </span><span class="lit">10</span><span class="pln"> </span><span class="pun">==</span><span class="pln"> </span><span class="lit">0</span><span class="pun">);</span><span class="pln">
        </span><span class="kwd">return</span><span class="pln"> $valid</span><span class="pun">;</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span>
登录后复制

文章摘自:php信用卡卡号验证函数


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板