Home  >  Article  >  php教程  >  PHP实例:PHP取GB2312编码字符串首字母的方法

PHP实例:PHP取GB2312编码字符串首字母的方法

WBOY
WBOYOriginal
2016-06-21 09:01:311066browse

/*
* @author: zhuyubing@gmail.com
*/
$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,
);
//取GB2312字符串首字母,原理是GBK汉字是按拼音顺序编码的.
function get_letter($input)
{
global $dict;
$str_1 = substr($input, 0, 1);
if ($str_1 >= chr(0x81) && $str_1 $num = hexdec(bin2hex(substr($input, 0, 2)));
foreach ($dict as $k=>$v){
if($v>=$num)
break;
}
return $k;
}
else{
return $str_1;
}
}

echo get_letter('中');
echo get_letter('华');
echo get_letter('人');
echo get_letter('民');
echo get_letter('共');
echo get_letter('和');
echo get_letter('国');
echo get_letter('万岁');
echo get_letter('c');
echo get_letter('h');
echo get_letter('i');
echo get_letter('n');
echo get_letter('a');
/**/
?>



Statement:
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