3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php <?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; // 奇偶数相加 $num = strval($num); $res = $num{strlen($num) -1}; 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 '验证失败'; }
Output for 5.3.0 - 5.4.30
Parse error: syntax error, unexpected '<' in /in/JPFep on line 3
Process exited with code 255.