相關知識儲備php
例如最後一個碼位是9494,區號和位號分別轉換成十六進制是5E5E,0x5E+0xA0=0xFE,因此該碼位的GB2312編碼是FEFE。編碼
<?php //國標碼與區位碼轉換常量 $GB_SP_DIFF = 160; //存放國標一級漢字不一樣讀音的起始 區位碼 $secPosValueList = array(1601, 1637, 1833, 2078,2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027, 4086, 4390, 4558, 4684, 4925, 5249, 5600); //存放國標一級漢字不一樣讀音的起始區位碼對應讀音 $firstLetter = array('a', 'b', 'c', 'd', 'e', 'f','g', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'w', 'x', 'y', 'z'); $string = "你們好"; echo $string.'<br/>'; $string=iconv( "UTF-8", "gb2312//IGNORE" , $string); $result = array(); //十進制 for($i=0;$i<strlen($string);$i++){ if(ord($string[$i])>127){ $result[] = (ord($string[$i])-$GB_SP_DIFF).''.(ord($string[++$i])-$GB_SP_DIFF); //從十進制 -160 獲得區位碼 } } //將區位碼轉對應的讀音 $str=''; for($i=0;$i<count($result);$i++){ $j=getLetter($secPosValueList,$result[$i],count($secPosValueList),0); $str.=$firstLetter[$j]; } echo '首字母:'.$str; //二分查找 function getLetter($arr,$val,$hight,$low){ while($low <= $hight){ $mid = ceil($low + ($hight - $low) / 2); if($arr[$mid] <= $val && $arr[$mid+1] >$val){ return $mid; }elseif($arr[$mid] > $val){ $hight = $mid -1; }else{ $low = $mid +1; } } return -1; } ?>
運行結果:spa
你們好
首字母:djhcode