-
PHP Validate UPC and EAN13 Barcodes
As I couldn't find any routines to do this...
I hacked these up in Perl an age ago... needed PHP versions so here they are (Ask if you want the Perl versions lazy..)
function validate_UPCABarcode($barcode)
{
// check to see if barcode is 12 digits long
if(!preg_match("/^[0-9]{12}$/",$barcode)) {
return false;
}
$digits = $barcode;
// 1. sum each of the odd numbered digits
$odd_sum = $digits[0] + $digits[2] + $digits[4] + $digits[6] + $digits[8] + $digits[10];
// 2. multiply result by three
$odd_sum_three = $odd_sum * 3;
// 3. add the result to the sum of each of the even numbered digits
$even_sum = $digits[1] + $digits[3] + $digits[5] + $digits[7] + $digits[9];
$total_sum = $odd_sum_three + $even_sum;
// 4. subtract the result from the next highest power of 10
$next_ten = (ceil($total_sum/10))*10;
$check_digit = $next_ten - $total_sum;// if the check digit and the last digit of the barcode are OK return true;
if($check_digit == $digits[11]) {
return true;
}
return false;
}function validate_EAN13Barcode($barcode)
{
// check to see if barcode is 13 digits long
if(!preg_match("/^[0-9]{13}$/",$barcode)) {
return false;
}$digits = $barcode;
// 1. Add the values of the digits in the even-numbered positions: 2, 4, 6, etc.
$even_sum = $digits[1] + $digits[3] + $digits[5] + $digits[7] + $digits[9] + $digits[11];
// 2. Multiply this result by 3.
$even_sum_three = $even_sum * 3;
// 3. Add the values of the digits in the odd-numbered positions: 1, 3, 5, etc.
$odd_sum = $digits[0] + $digits[2] + $digits[4] + $digits[6] + $digits[8] + $digits[10];
// 4. Sum the results of steps 2 and 3.
$total_sum = $even_sum_three + $odd_sum;
// 5. The check character is the smallest number which, when added to the result in step 4, produces a multiple of 10.
$next_ten = (ceil($total_sum/10))*10;
$check_digit = $next_ten - $total_sum;// if the check digit and the last digit of the barcode are OK return true;
if($check_digit == $digits[12]) {
return true;
}return false;
}
leave a comment
2007.11.27 00:00 GMT
Looks great. Could you please email me the perl version. Yes, I am lazy.
Thanks
2007.11.28 00:00 GMT
hopefully these still work
sub checkUPCABarcode { my ($barcode) = @_; if ( !($barcode =~ /^[0-9]{12}$/) ) { return 0; } @digits = split(//,$barcode); # -- sum each of the odd numbered digits # remember perl arrays start at zero! $odd_sum = $digits[0] + $digits[2] + $digits[4] + $digits[6] + $digits[8] + $digits[10]; # -- multiply result by three $odd_sum_three = $odd_sum * 3; # -- add the result to the sum of each of the even numbered digits $even_sum = $digits[1] + $digits[3] + $digits[5] + $digits[7] + $digits[9]; $total_sum = $odd_sum_three + $even_sum; # -- subtract the result from the next highest power of 10 $next_ten = (ceil($total_sum/10))*10; $check_digit = $next_ten - $total_sum;
# if the check digit and the last digit of the barcode are OK return true; if ( $check_digit == $digits[11] ) { return 1; }
return 0; }
sub checkEAN13Barcode { my ($barcode) = @_; if ( !($barcode =~ /^[0-9]{13}$/) ) { return 0; } @digits = split(//,$barcode);
# 1. Add the values of the digits in the even-numbered positions: 2, 4, 6, etc. # remember perl arrays start at zero! $even_sum = $digits[1] + $digits[3] + $digits[5] + $digits[7] + $digits[9] + $digits[11]; # 2. Multiply this result by 3. $even_sum_three = $even_sum * 3; # 3. Add the values of the digits in the odd-numbered positions: 1, 3, 5, etc. $odd_sum = $digits[0] + $digits[2] + $digits[4] + $digits[6] + $digits[8] + $digits[10]; # 4. Sum the results of steps 2 and 3. $total_sum = $even_sum_three + $odd_sum; # 5. The check character is the smallest number which, when added to the result in step 4, produces a multiple of 10. $next_ten = (ceil($total_sum/10))*10; $check_digit = $next_ten - $total_sum;
# if the check digit and the last digit of the barcode are OK return true; if ( $check_digit == $digits[12] ) { return 1; }
return 0; }
2008.02.20 09:42 GMT
Here is a PostScript routine that calculates the EAN13 check digit
%!PS % ------------------------------------------------------------------ % (12-character numeric string) EAN13.CheckDigit -> (checkCharacter) % ------------------------------------------------------------------ /EAN13.CheckDigit { 5 dict begin /inputString exch def /checkDigit 0 def /factor 1 def /s 3 string def 0 1 11 { /i exch def /checkDigit checkDigit inputString i 1 getinterval cvi factor mul add def /factor factor 2 xor def } for /checkDigit checkDigit 10 mod def checkDigit 0 ne { /checkDigit 10 checkDigit sub def } if checkDigit s cvs end } def % ------------------------------------------------------------------
% Test (these are real life EANs) (900310607223) dup == EAN13.CheckDigit == % 1 (900310650559) dup == EAN13.CheckDigit == % 3 (380000110009) dup == EAN13.CheckDigit == % 2 (590471440187) dup == EAN13.CheckDigit == % 8 (380005170002) dup == EAN13.CheckDigit == % 0 (501753491820) dup == EAN13.CheckDigit == % 1 (692098249717) dup == EAN13.CheckDigit == % 6 (544900003309) dup == EAN13.CheckDigit == % 3
2011.01.17 12:08 GMT
Can I use this in a gpl-licenced project? https://github.com/HansF/Fridgemagnet
2011.11.10 12:00 GMT
Improved Perl version ("strict" friendly, includes the required POSIX module, slight syntax modifications). Thanks to the original poster.
- Phil
sub UPCACodeIsValid {
use POSIX qw(ceil);
my $barcode = shift;
return 0 if ( !($barcode =~ /^[0-9]{12}$/) );
my @digits = split(//, $barcode);
# -- sum each of the odd numbered digits
# remember perl arrays start at zero!
my $odd_sum = $digits[0] + $digits[2] + $digits[4] + $digits[6] + $digits[8] + $digits[10];
# -- multiply result by three
my $odd_sum_three = $odd_sum * 3;
# -- add the result to the sum of each of the even numbered digits
my $even_sum = $digits[1] + $digits[3] + $digits[5] + $digits[7] + $digits[9];
my $total_sum = $odd_sum_three + $even_sum;
# -- subtract the result from the next highest power of 10
my $next_ten = (ceil($total_sum/10))*10;
my $check_digit = $next_ten - $total_sum;
# if the check digit and the last digit of the barcode are OK return true;
return 1 if ( $check_digit == $digits[11] );
return 0;
}
#################################################
sub EAN13CodeIsValid {
use POSIX qw(ceil);
my $barcode = shift;
return 0 if ( !($barcode =~ /^[0-9]{13}$/) );
my @digits = split(//, $barcode);
# 1. Add the values of the digits in the even-numbered positions: 2, 4, 6, etc.
# remember perl arrays start at zero!
my $even_sum = $digits[1] + $digits[3] + $digits[5] + $digits[7] + $digits[9] + $digits[11];
# 2. Multiply this result by 3.
my $even_sum_three = $even_sum * 3;
# 3. Add the values of the digits in the odd-numbered positions: 1, 3, 5, etc.
my $odd_sum = $digits[0] + $digits[2] + $digits[4] + $digits[6] + $digits[8] + $digits[10];
# 4. Sum the results of steps 2 and 3.
my $total_sum = $even_sum_three + $odd_sum;
# 5. The check character is the smallest number which, when added to the result in step 4, produces a multiple of 10.
my $next_ten = (ceil($total_sum/10))*10;
my $check_digit = $next_ten - $total_sum;
# if the check digit and the last digit of the barcode are OK return true;
return 1 if ( $check_digit == $digits[12] );
return 0;
}