Encountered a Chinese string sorting problem, to get the first letter of Chinese, the program can get the first letter of any string. The principle is that according to the gb2312 encoding is sorted by pinyin. Unfamiliar with gb2312, you can look at the coding method. Source: http://www.phpman.cn
-
- $limit =array (//gb2312 pinyin sort
- Array (45217,45252),//a
- Array (45253,45760),//b
- Array (45761,46317),//c
- Array (46318,46825),//d
- Array (46826,47009),//e
- Array (47010,47296),//f
- Array (47297,47613),//g
- Array (47614,48118),//h
- Array (0,0),//i
- Array (48119,49061),//j
- Array (49062,49323),//k
- Array (49324,49895),//l
- Array (49896,50370),//m
- Array (50371,50613),//n
- Array (50614,50621),//o
- Array (50622,50905),//p
- Array (50906,51386),//q
- Array (51387,51445),//r
- Array (51446,52217),//s
- Array (52218,52697),//t
- Array (0,0),//u
- Array (0,0),//v
- Array (52698,52979),//w
- Array (52980,53688),//x
- Array (53689,54480),//y
- Array (54481,55289),//z
- );
- $str = "A: This is a test program 1";
- $str = Iconv ("UTF-8", "gb2312", $str);
- echo $str. "
";
- $i = 0;
- while ($i
- $tmp =bin2hex (substr ($str, $i, 1));
- if ($tmp >= ' B0 ') {//start of Kanji
- $t =getletter (Hexdec (Bin2Hex (substr ($str, $i, 2)));
- printf ("%c", $t ==-1? ' * ': $t);
- $i +=2;
- }
- else{
- printf ("%s", substr ($str, $i, 1));
- $i + +;
- }
- }
- function Getletter ($num) {
- Global $limit;
- $char _index=65;
- foreach ($limit as $k = = $v) {
- if ($num >= $v [0] && $num <= $v [1]) {
- $char _index+= $k;
- return $char _index;
- }
- }
- return-1;
- }
- ?>
Copy Code |