Maison > développement back-end > tutoriel php > php信誉卡卡号验证函数

php信誉卡卡号验证函数

WBOY
Libérer: 2016-06-13 12:37:09
original
844 Les gens l'ont consulté

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>
Copier après la connexion

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


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal