Rumah >pembangunan bahagian belakang >tutorial php > php信誉卡卡号验证函数

php信誉卡卡号验证函数

WBOY
WBOYasal
2016-06-13 12:37:09853semak imbas

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信用卡卡号验证函数


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn