Euro banknote validation


/ Published in: PHP
Save to your folder(s)

Basically what this algorithm does is to replace the first letter of the serial number by a number (each letter represents where the banknote is originally from), then it calculates the sum of all digits. If the remaining of the division of that sum by 9 is 0 then your note is valid.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. /**
  4.  * Checks if the input string is a valid Euro note serial number.
  5.  *
  6.  * @param string $string
  7.  * @return bool
  8.  */
  9. function Euro($string)
  10. {
  11. settype($string, 'string');
  12.  
  13. if (strlen($string) != 12)
  14. {
  15. return false;
  16. }
  17.  
  18. $string = str_replace(array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'), array(2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9), strtoupper($string));
  19.  
  20. $stack = 0;
  21.  
  22. for ($i = 0; $i < 12; $i++)
  23. {
  24. $stack += $string[$i];
  25. }
  26.  
  27. if ($stack % 9 == 0)
  28. {
  29. return true;
  30. }
  31.  
  32. return false;
  33. }
  34.  
  35. ?>

URL: http://www.alixaxel.com/wordpress/2007/06/23/euro-banknote-validation-with-php/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.