介绍一个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">++)</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">+</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信用卡卡号验证函数