码迷,mamicode.com
首页 > 编程语言 > 详细

后台银行卡算法

时间:2017-06-26 13:43:59      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:type   list   bool   func   car   count   class   public   false   

 /**
     * [cardList 银行卡号是否正确]
     * luha 算法
     * @return [type]  bool
     */
    public static function check_card($no)
    {
        $return = false;
        $arr_no = str_split($no);

        $last_n = $arr_no[count($arr_no)-1];

        krsort($arr_no);

        $i = 1;
        $total = 0;

        foreach ($arr_no as $n){

            if($i%2==0){

                $ix = $n*2;

                if($ix>=10){

                    $nx = 1 + ($ix % 10);
                    $total += $nx;

                }else{

                    $total += $ix;

                }

            }else{

                $total += $n;

            }

            $i++;
        }

        $total -= $last_n;
        $total *= 9;

        if($last_n == ($total%10)){
            $return = true;
        }

        return $return;
    }

返回值:false表示默认不通过,true是通过

后台银行卡算法

标签:type   list   bool   func   car   count   class   public   false   

原文地址:http://www.cnblogs.com/hanshuai0921/p/7079878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!