Advertisement
konyakov

ean13 check digit

Jul 20th, 2013
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.40 KB | None | 0 0
  1. <?php
  2.  
  3. function ean_checkdigit($barcode){
  4.         $barcode = str_pad($barcode, 12, "0", STR_PAD_LEFT);
  5.         $sum = 0;
  6.         for($i=(strlen($barcode)-1);$i>=0;$i--){
  7.                 $sum += (($i % 2) * 2 + 1 ) * $barcode[$i];
  8.         }
  9.         return (10 - ($sum % 10));
  10. }
  11.  
  12. $ean13 = '201311247898';
  13.  
  14. $ean13 .= ean_checkdigit($ean13); //now $ean13 = 2013112478986
  15.  
  16. echo "EAN-13 = $ean13";
  17.  
  18. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement