微信公众号定制开发

社会统一信用代码校验方法——php实现
<pre class="prettyprint lang-php">/** * 判断是否为统一社会信用代码 */ function isCompanyCode($str){ // 总共18位 if(strlen($str) != 18){ return false; } $one = '159Y';//第一位可以出现的字符 $two = '12391';//第二位可以出现的字符 $str = strtoupper($str); if (!strstr($one, $str['1']) &amp;&amp; !strstr($two, $str['2']) &amp;&amp; !empty($array[substr($str, 2, 6)])) { return false; } //加权因子数值 $wi = array(1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28); $str_organization = substr($str, 0, 17); $num = 0; for ($i = 0; $i &lt; 17; $i++) { $temp = transformation($str_organization[$i]); if(!$temp){ continue; } $num += $temp* $wi[$i]; } switch ($num % 31) { case '0': $result = 0; break; default: $result = 31 - $num % 31; break; } if (substr($str, -1, 1) == transformation($result, true)) { return true; } else { return false; } } function transformation($num, $status = false){ //值转换 $list = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A' =&gt; 10, 'B' =&gt; 11, 'C' =&gt; 12, 'D' =&gt; 13, 'E' =&gt; 14, 'F' =&gt; 15, 'G' =&gt; 16, 'H' =&gt; 17, 'J' =&gt; 18, 'K' =&gt; 19, 'L' =&gt; 20, 'M' =&gt; 21, 'N' =&gt; 22, 'P' =&gt; 23, 'Q' =&gt; 24, 'R' =&gt; 25, 'T' =&gt; 26, 'U' =&gt; 27, 'W' =&gt; 28, 'X' =&gt; 29, 'Y' =&gt; 30); if ($status == true) { $list = array_flip($list); } return $list[$num] ?? ''; }</pre>

上一篇:二分查找

下一篇:没有更多文章了