Skip to content

Instantly share code, notes, and snippets.

@Modicrumb
Created June 27, 2013 15:48
Show Gist options
  • Save Modicrumb/5877603 to your computer and use it in GitHub Desktop.
Save Modicrumb/5877603 to your computer and use it in GitHub Desktop.
/* 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