| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2018/1/19/019
- * Time: 17:23
- */
- namespace common\helpers;
- class BankCardHelper
- {
- /**
- * @var BankCardHelper
- */
- private static $_instance;
- /**
- * BankCardHelper constructor.
- */
- private function __construct()
- {
- }
-
- /**
- * 验证银行卡号的正确性luhn算法
- * @param $card_number
- * @return bool
- */
- public function check_bankCard($card_number)
- {
- $arr_no = str_split($card_number);
- $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;
- $x = 10 - ($total % 10);
- if ($x == $last_n) {
- return true;
- } else {
- return false;
- }
- }
- public static function getInstance()
- {
- if (self::$_instance === null) {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- }
|