• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

php实现的信用卡识别和验证类

PHP 水墨上仙 1191次浏览

这个php类可以识别和验证不同类型的信息卡号码

<?php 
class credit_card 
{ 
    function clean_no ($cc_no) 
    { 
        // Remove non-numeric characters from $cc_no   
        return ereg_replace ('[^0-9]+', '', $cc_no); 
    } 
    function identify ($cc_no) 
    { 
         $cc_no = credit_card::clean_no ($cc_no); 
        // Get card type based on prefix and length of card number   
        if (ereg ('^4(.{12}|.{15})$', $cc_no)) 
            return 'Visa';   
        if (ereg ('^5[1-5].{14}$', $cc_no)) 
            return 'Mastercard'; 
        if (ereg ('^3[47].{13}$', $cc_no)) 
            return 'American Express'; 
        if (ereg ('^3(0[0-5].{11}|[68].{12})$', $cc_no)) 
            return 'Diners Club/Carte Blanche'; 
        if (ereg ('^6011.{12}$', $cc_no)) 
            return 'Discover Card'; 
        if (ereg ('^(3.{15}|(2131|1800).{11})$', $cc_no)) 
            return 'JCB'; 
        if (ereg ('^2(014|149).{11})$', $cc_no)) 
            return 'enRoute'; 
        return 'unknown'; 
    } 
    function validate ($cc_no) 
    { 
        // Reverse and clean the number 
        $cc_no = strrev (credit_card::clean_no ($cc_no)); 
          
        // VALIDATION ALGORITHM 
        // Loop through the number one digit at a time 
        // Double the value of every second digit (starting from the right) 
        // Concatenate the new values with the unaffected digits 
        for ($ndx = 0; $ndx < strlen ($cc_no); ++$ndx) 
            $digits .= ($ndx % 2) ? $cc_no[$ndx] * 2 : $cc_no[$ndx]; 
          
        // Add all of the single digits together 
        for ($ndx = 0; $ndx < strlen ($digits); ++$ndx) 
            $sum += $digits[$ndx]; 
        // Valid card numbers will be transformed into a multiple of 10 
        return ($sum % 10) ? FALSE : TRUE; 
    } 
    function check ($cc_no) 
    { 
        $valid = credit_card::validate ($cc_no); 
        $type  = credit_card::identify ($cc_no); 
        return array ($valid, $type, 'valid' => $valid, 'type' => $type); 
    } 
} 
?>


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明php实现的信用卡识别和验证类
喜欢 (0)
加载中……