Posted By

Scooter on 03/07/09


Tagged


Versions (?)


Advertising

Website Promotion DIRECTORY is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


Who likes this?

1 person has marked this snippet as a favorite

metal


Luhn Algorithm


Published in: PHP 






URL: http://reusablecode.blogspot.com/2009/03/luhn-algorithm.html

For verifying credit card numbers.

Expand | Embed | Plain Text
  1. <?php
  2. /*
  3.   Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  4.  
  5.   This work is licensed under the Creative Commons Attribution License. To view
  6.   a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  7.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  8.   94305, USA.
  9.   */
  10.  
  11. // Luhn (mod 10) algorithm
  12. function luhn($input)
  13. {
  14. $sum = 0;
  15. $odd = strlen($input) % 2;
  16.  
  17. // Remove any non-numeric characters.
  18. if (!is_numeric($input))
  19. {
  20. eregi_replace("\D", "", $input);
  21. }
  22.  
  23. // Calculate sum of digits.
  24. for($i = 0; $i < strlen($input); $i++)
  25. {
  26. $sum += $odd ? $input[$i] : (($input[$i] * 2 > 9) ? $input[$i] * 2 - 9 : $input[$i] * 2);
  27. $odd = !$odd;
  28. }
  29.  
  30. // Check validity.
  31. return ($sum % 10 == 0) ? true : false;
  32. }
  33.  
  34. // Unit test - expected result is true.
  35. echo (luhn("446667651")) ? "true" : "false";
  36. ?>

Report this snippet 

You need to login to post a comment.

Download royalty free graphics