Heim > Backend-Entwicklung > PHP-Tutorial > php信誉卡卡号验证函数

php信誉卡卡号验证函数

WBOY
Freigeben: 2016-06-13 12:37:09
Original
859 Leute haben es durchsucht

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>
Nach dem Login kopieren

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


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage