Return to Snippet

Revision: 4625
at January 13, 2008 20:10 by j_junyent


Initial Code
<?php

/**
 * Checks if the input string is a valid Euro note serial number.
 *
 * @param string $string
 * @return bool
 */
function Euro($string)
{
	settype($string, 'string');

	if (strlen($string) != 12)
	{
		return false;
	}

	$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));

	$stack = 0;

	for ($i = 0; $i < 12; $i++)
	{
		$stack += $string[$i];
	}

	if ($stack % 9 == 0)
	{
		return true;
	}

	return false;
}

?>

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

Initial Description
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.

Initial Title
Euro banknote validation

Initial Tags
validation

Initial Language
PHP