Guest User

Untitled

a guest
Jul 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.75 KB | None | 0 0
  1. <?php
  2.     function isValid ($arg)
  3.     {
  4.         if (strlen ($arg) != 13)
  5.             return false;
  6.  
  7.         $chk = false; //true isvalid false is invalid
  8.         $str = strtoupper ($arg);
  9.         $checkBar = true;
  10.  
  11.         if ($str{10} == "X")
  12.         {
  13.             $checkBar = false;
  14.             $chk = true;
  15.         }
  16.        
  17.         if ($checkBar)
  18.         {
  19.             $sum = 0; //to check barcode
  20.             $sum += (int)$str{2}*8;
  21.             $sum += (int)$str{3}*6;
  22.             $sum += (int)$str{4}*4;
  23.             $sum += (int)$str{5}*2;
  24.             $sum += (int)$str{6}*3;
  25.             $sum += (int)$str{7}*5;
  26.             $sum += (int)$str{8}*9;
  27.             $sum += (int)$str{9}*7;
  28.             $sum %= 11;
  29.  
  30.             if ($sum == 0)
  31.             {
  32.                 if ($str{10} == "5")
  33.                     $chk = true;
  34.                 else
  35.                     return false;
  36.             }
  37.             else
  38.                 if ($sum == 1)
  39.                 {
  40.                     if ($str{10} == "0")
  41.                         $chk = true;
  42.                     else
  43.                         return false;
  44.                 }
  45.                 else
  46.                     if ((int)$str{10} == (11-$sum))
  47.                         $chk = true;
  48.                     else
  49.                         return false;
  50.         }
  51.  
  52.         switch ($str{0})
  53.         {
  54.             case "E" :
  55.             case "C" :
  56.             case "R" :
  57.             case "L" : return true;
  58.         }
  59.  
  60.  
  61.         if (substr ($str,0,2) == "DS")
  62.             return true;
  63.         if ($str{0} == "V" && substr ($str,11,2) != "TH")
  64.             return true;
  65.         if (substr ($str,0,2) == "PE" && substr ($str,11,2) != "TH")
  66.             return true;
  67.         return false;
  68.     }
  69. ?>
Add Comment
Please, Sign In to add comment