Created
June 27, 2013 15:48
-
-
Save Modicrumb/5877603 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Import Function */ | |
function is_upca($upc) | |
{ | |
if (strlen((string)$upc)==12) | |
{ | |
//commence extra steps | |
if (preg_match('/[a-z]/i',$upc)) | |
{ | |
return false; | |
} | |
else | |
{ | |
$upc_arr = str_split($upc); | |
//take last element off | |
$ld = array_pop($upc_arr); | |
$od = true; //indexer to get odds | |
$odds = 0; | |
$evens = 0; | |
//keep in mind we only want numbers 1-11 of the array, not 1-12 | |
foreach ($upc_arr as $i) | |
{ | |
if ($od = !$od) | |
{ | |
$evens += (int)$i; | |
} | |
else | |
{ | |
$odds += (int)$i; | |
} | |
} | |
//now we can use odds and evens to check the last digit | |
//formula for check digit | |
$Mo = (($odds*3)+ $evens) % 10; | |
print $Mo; | |
if ($Mo == 0) | |
{ | |
$toCheck = 0; | |
} | |
else | |
{ | |
$toCheck = 10- $Mo; | |
} | |
if ((int)$ld == $toCheck ) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/*End Import */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment