Last active
August 29, 2015 14:03
-
-
Save mingyun/d81b9e4f59803c1db7c3 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
<?php | |
/** | |
* 检验药品本位码 | |
* 规则: | |
* 全国产品与服务统一代码由 13位数字本体代码和 1位数字校验码组成 | |
* 第一步:按照由右至左的顺序,确定代码中包括校验码在内的各位代码的代码位置序号(校验码的GB 18937- 2003 | |
* 代码位置序号为 1,其他由右至左依次为 2,3,4,14) ; | |
* 第二步:从代码位置序号为2的位置开始,求所有偶数位代码的和; | |
* 第三步:将第二步的和乘以3; | |
* 第四步:从代码位置序号为 3的位置开始,求除了代码位置序号为 1的其他所有奇数位代码的和; | |
* 第五步:将第三步与第四步的结果相加; | |
* 第六步:如果第五步所得的和的个位数为零,则校验码为 。;如果第五步所得的和的个位数不为零, | |
* 则用“10”减去第五步所得的和的个位数,所得的差为校验码。 | |
*/ | |
class Check | |
{ | |
/** | |
* 过滤和验证 | |
* @param str $str 待验证的字符 | |
* @return void | |
*/ | |
public function validStr($str){ | |
$str = $this ->convert($str); | |
if(preg_match('#^869\d{11}$#', $str)) { | |
$arr = str_split(substr($str, 0,13)); | |
$even = $odd = $num = 0; | |
foreach ($arr as $key => $value) { | |
if(0 == $key % 2) { | |
$even += $value; // 偶数位数字相加 | |
} else { | |
$odd += $value; // 奇数位数字相加 | |
} | |
} | |
$even *= 3; | |
$num = $even + $odd; // 奇偶数相加 | |
$res = $num % 10; | |
if(0 == $res && 0 == $str[13]) { | |
return TRUE; | |
} else if ($str[13] == (10 - $res)) { | |
return TRUE; | |
} else { | |
return FALSE; | |
} | |
} else { | |
return FALSE; | |
} | |
} | |
public function convert($str) | |
{ | |
$arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', | |
'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9', | |
'A' => 'A', 'B' => 'B', 'C' => 'C', 'D' => 'D', 'E' => 'E', | |
'F' => 'F', 'G' => 'G', 'H' => 'H', 'I' => 'I', 'J' => 'J', | |
'K' => 'K', 'L' => 'L', 'M' => 'M', 'N' => 'N', 'O' => 'O', | |
'P' => 'P', 'Q' => 'Q', 'R' => 'R', 'S' => 'S', 'T' => 'T', | |
'U' => 'U', 'V' => 'V', 'W' => 'W', 'X' => 'X', 'Y' => 'Y', | |
'Z' => 'Z', 'a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', | |
'e' => 'e', 'f' => 'f', 'g' => 'g', 'h' => 'h', 'i' => 'i', | |
'j' => 'j', 'k' => 'k', 'l' => 'l', 'm' => 'm', 'n' => 'n', | |
'o' => 'o', 'p' => 'p', 'q' => 'q', 'r' => 'r', 's' => 's', | |
't' => 't', 'u' => 'u', 'v' => 'v', 'w' => 'w', 'x' => 'x', | |
'y' => 'y', 'z' => 'z', '》' => '>', '*'=>'*' ,'"'=>'"', | |
'(' => '(', ')' => ')', '〔' => '[', '〕' => ']', '【' => '[', | |
'】' => ']', '〖' => '[', '〗' => ']', '“' => '[', '”' => ']', | |
'‘' => '[', '’' => ']', '{' => '{', '}' => '}', '《' => '<', | |
'%' => '%', '+' => '+', '—' => '-', '-' => '-', '~' => '-', | |
':' => ':', '。' => '.', '、' => ',', ',' => '.', '、' => '.', | |
';' => ',', '?' => '?', '!' => '!', '…' => '-', '‖' => '|', | |
'”' => '"', '’' => '`', '‘' => '`', '|' => '|', '〃' => '"', | |
' ' => ' ', '$'=>'$', '@'=>'@', '#'=>'#', '^'=>'^', '&'=>'&', | |
'-'=>'', '/'=>''); | |
return strtr($str, $arr); | |
} | |
} | |
// example | |
$check = new check(); | |
$str = '8-69-12345-54321-3'; | |
if ($check ->validStr($str)) { | |
echo '验证成功'; | |
} else { | |
echo '验证失败'; | |
} | |
//via:http://3v4l.org/JPFep | |
/** | |
* 检验药品本位码 | |
* 规则: | |
* 全国产品与服务统一代码由 13位数字本体代码和 1位数字校验码组成 | |
* 第一步:按照由右至左的顺序,确定代码中包括校验码在内的各位代码的代码位置序号(校验码的GB 18937- 2003) | |
* 代码位置序号为 1,其他由右至左依次为 2,3,4...14) ; | |
* 第二步:从代码位置序号为2的位置开始,求所有偶数位代码的和; | |
* 第三步:将第二步的和乘以3; | |
* 第四步:从代码位置序号为 3的位置开始,求除了代码位置序号为 1的其他所有奇数位代码的和; | |
* 第五步:将第三步与第四步的结果相加; | |
* 第六步:如果第五步所得的和的个位数为零,则校验码为0;如果第五步所得的和的个位数不为零, | |
* 则用“10”减去第五步所得的和的个位数,所得的差为校验码。 | |
*/ | |
class Check | |
{ | |
/** | |
* 过滤和验证 | |
* @param str $str 待验证的字符 | |
* @return void | |
*/ | |
public function validStr($str) | |
{ | |
$str = $this->convert($str); | |
echo $str = preg_replace('#\D+#','',$str); | |
/*for ($i = 0;$i < strlen($str);$i++) { | |
if(!preg_match('#\d+#', $str[$i])) { | |
$str = str_replace($str[$i], '',$str); | |
} | |
}*/ | |
if(!preg_match('#^869\d{11}$#', $str)) { | |
return FALSE; | |
} | |
//$arr = str_split(substr($str, 0,13)); | |
$even = $odd = $num = 0; | |
/*foreach ($arr as $key => $value) { | |
if(0 == $key % 2) { | |
$even += $value; // 偶数位数字相加 | |
} else { | |
$odd += $value; // 奇数位数字相加 | |
} | |
}*/ | |
for ($i = 0;$i < 13;$i++) { | |
if(0 == $i % 2) { | |
$even += $str[$i]; // 偶数位数字相加 | |
} else { | |
$odd += $str[$i]; // 奇数位数字相加 | |
} | |
} | |
$even *= 3; | |
$num = $even + $odd; // 奇偶数相加 | |
$res = $num % 10; | |
if(0 == $res && 0 == $str[13]) { | |
return TRUE; | |
} else if ($str[13] == (10 - $res)) { | |
return TRUE; | |
} else { | |
return FALSE; | |
} | |
} | |
// 字符转换 | |
public function convert($str) | |
{ | |
$arr = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', | |
'5' => '5', '6' => '6', '7' => '7', '8' => '8', '9' => '9'); | |
return strtr($str, $arr); | |
} | |
} | |
// example | |
$check = new check(); | |
$str = '8-691-@#23y455gy43213'; | |
if ($check ->validStr($str)) { | |
echo '验证成功'; | |
} else { | |
echo '验证失败'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment