BankCardHelper.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/1/19/019
  6. * Time: 17:23
  7. */
  8. namespace common\helpers;
  9. class BankCardHelper
  10. {
  11. /**
  12. * @var BankCardHelper
  13. */
  14. private static $_instance;
  15. /**
  16. * BankCardHelper constructor.
  17. */
  18. private function __construct()
  19. {
  20. }
  21. /**
  22. * 验证银行卡号的正确性luhn算法
  23. * @param $card_number
  24. * @return bool
  25. */
  26. public function check_bankCard($card_number)
  27. {
  28. $arr_no = str_split($card_number);
  29. $last_n = $arr_no[count($arr_no)-1];
  30. krsort($arr_no);
  31. $i = 1;
  32. $total = 0;
  33. foreach ($arr_no as $n) {
  34. if ($i%2 == 0) {
  35. $ix = $n * 2;
  36. if ($ix >= 10) {
  37. $nx = 1 + ($ix % 10);
  38. $total += $nx;
  39. } else {
  40. $total += $ix;
  41. }
  42. } else {
  43. $total += $n;
  44. }
  45. $i++;
  46. }
  47. $total -= $last_n;
  48. $x = 10 - ($total % 10);
  49. if ($x == $last_n) {
  50. return true;
  51. } else {
  52. return false;
  53. }
  54. }
  55. public static function getInstance()
  56. {
  57. if (self::$_instance === null) {
  58. self::$_instance = new self();
  59. }
  60. return self::$_instance;
  61. }
  62. }