Guest User

Untitled

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