PHP implementation code to obtain the first letter of Chinese characters (gb2312 encoding)

WBOY
Release: 2016-07-25 08:57:13
Original
1046 people have browsed it
This article introduces how to use PHP to obtain the first letter of a Chinese character. The encoding format is gb2312. Friends in need can refer to it.

php gets the first letter of Chinese characters, code:

<?php
//取GB2312字符串首字母
//GBK汉字是按拼音顺序编码的
function get_letter($input){
$dict=array(
'a'=>0xB0C4,
'b'=>0xB2C0,
'c'=>0xB4ED,
'd'=>0xB6E9,
'e'=>0xB7A1,
'f'=>0xB8C0,
'g'=>0xB9FD,
'h'=>0xBBF6,
'j'=>0xBFA5,
'k'=>0xC0AB,
'l'=>0xC2E7,
'm'=>0xC4C2,
'n'=>0xC5B5,
'o'=>0xC5BD,
'p'=>0xC6D9,
'q'=>0xC8BA,
'r'=>0xC8F5,
's'=>0xCBF9,
't'=>0xCDD9,
'w'=>0xCEF3,
'x'=>0xD188,
'y'=>0xD4D0,
'z'=>0xD7F9,
);

$str_1 = substr($input, 0, 1);
if ($str_1 >= chr(0x81) && $str_1 <= chr(0xfe))    {
$num = hexdec(bin2hex(substr($input, 0, 2)));
foreach ($dict as $k=>$v){
if($v>=$num)
break;
}
return $k;
}
else{
return $str_1;
} //by bbs.it-home.org
}

echo get_letter('脚');
echo get_letter('本');
echo get_letter('学');
echo get_letter('堂');
echo get_letter('欢');
echo get_letter('迎');
echo get_letter('您');
echo get_letter('网站编程');
?>
Copy after login

This is a pretty good piece of code, especially suitable for taking the first letter in the title and generating a list of articles classified by letters.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template