Generate valid EAN13 in PHP

Like you said, 200 (actually the entire 200 - 299 range) is a fake country code that indicates that the number is an internal number. That means that you can make up the rest of the number.

An EAN number is typically 13 digits long, although it can have other lengths as well. Let's keep it at 13, since you specified the want for EAN13.

The 13th digit is a check digit. So all you have to do, is perform the same validation but with a twist. Instead of calculating the check digit and validate if the last digit matches the check digit, you just calculate the check digit and add it to the 12 digits you already have.

I haven't really tried to modify your existing code, since it looks confusing and complex, but based on the validation rules I found on Wikipedia, this should do the trick:

function generateEAN($number)
{
  $code = '200' . str_pad($number, 9, '0');
  $weightflag = true;
  $sum = 0;
  // Weight for a digit in the checksum is 3, 1, 3.. starting from the last digit. 
  // loop backwards to make the loop length-agnostic. The same basic functionality 
  // will work for codes of different lengths.
  for ($i = strlen($code) - 1; $i >= 0; $i--)
  {
    $sum += (int)$code[$i] * ($weightflag?3:1);
    $weightflag = !$weightflag;
  }
  $code .= (10 - ($sum % 10)) % 10;
  return $code;
}

$number is the internal code you want to have EANed. The prefix, zero-padding and checksum are added by the function.

Tags:

Php