从网上找的一段代码,在调试的时候输入其他汉字 可以正确输出,唯独 这个 ‘好’字没法正确输出。为什么呢 ???
各位达人还有没有 更好的代码呢 ??如果有还请分享下哦 。
最好是 能够处理汉字词组,而不是单个汉字的。
<?phpfunction getfirstchar($s0){ if($fchar>=ord("a") and $fchar<=ord("Z") )return strtoupper($s0{0});
$s=iconv("UTF-8","gb2312", $s0);
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319 and $asc<=-20284)return "A";
if($asc>=-20283 and $asc<=-19776)return "B";
if($asc>=-19775 and $asc<=-19219)return "C";
if($asc>=-19218 and $asc<=-18711)return "D";
if($asc>=-18710 and $asc<=-18527)return "E";
if($asc>=-18526 and $asc<=-18240)return "F";
if($asc>=-18239 and $asc<=-17923)return "G";
if($asc>=-17922 and $asc<=-17418)return "I";
if($asc>=-17417 and $asc<=-16475)return "J";
if($asc>=-16474 and $asc<=-16213)return "K";
if($asc>=-16212 and $asc<=-15641)return "L";
if($asc>=-15640 and $asc<=-15166)return "M";
if($asc>=-15165 and $asc<=-14923)return "N";
if($asc>=-14922 and $asc<=-14915)return "O";
if($asc>=-14914 and $asc<=-14631)return "P";
if($asc>=-14630 and $asc<=-14150)return "Q";
if($asc>=-14149 and $asc<=-14091)return "R";
if($asc>=-14090 and $asc<=-13319)return "S";
if($asc>=-13318 and $asc<=-12839)return "T";
if($asc>=-12838 and $asc<=-12557)return "W";
if($asc>=-12556 and $asc<=-11848)return "X";
if($asc>=-11847 and $asc<=-11056)return "Y";
if($asc>=-11055 and $asc<=-10247)return "Z";
return null;
}echo getfirstchar("好");
?>
各位达人还有没有 更好的代码呢 ??如果有还请分享下哦 。
最好是 能够处理汉字词组,而不是单个汉字的。
<?phpfunction getfirstchar($s0){ if($fchar>=ord("a") and $fchar<=ord("Z") )return strtoupper($s0{0});
$s=iconv("UTF-8","gb2312", $s0);
$asc=ord($s{0})*256+ord($s{1})-65536;
if($asc>=-20319 and $asc<=-20284)return "A";
if($asc>=-20283 and $asc<=-19776)return "B";
if($asc>=-19775 and $asc<=-19219)return "C";
if($asc>=-19218 and $asc<=-18711)return "D";
if($asc>=-18710 and $asc<=-18527)return "E";
if($asc>=-18526 and $asc<=-18240)return "F";
if($asc>=-18239 and $asc<=-17923)return "G";
if($asc>=-17922 and $asc<=-17418)return "I";
if($asc>=-17417 and $asc<=-16475)return "J";
if($asc>=-16474 and $asc<=-16213)return "K";
if($asc>=-16212 and $asc<=-15641)return "L";
if($asc>=-15640 and $asc<=-15166)return "M";
if($asc>=-15165 and $asc<=-14923)return "N";
if($asc>=-14922 and $asc<=-14915)return "O";
if($asc>=-14914 and $asc<=-14631)return "P";
if($asc>=-14630 and $asc<=-14150)return "Q";
if($asc>=-14149 and $asc<=-14091)return "R";
if($asc>=-14090 and $asc<=-13319)return "S";
if($asc>=-13318 and $asc<=-12839)return "T";
if($asc>=-12838 and $asc<=-12557)return "W";
if($asc>=-12556 and $asc<=-11848)return "X";
if($asc>=-11847 and $asc<=-11056)return "Y";
if($asc>=-11055 and $asc<=-10247)return "Z";
return null;
}echo getfirstchar("好");
?>
if($asc>=-18239 and $asc<=-17923)return "G";
if($asc>=-17922 and $asc<=-17418)return "I"; 网上多数是根据gb2312的排列方法是按字母排列这一点,进行查找对照的。还有一些是根据汉字来匹配首字母,都差不多,没什么技术含量…以前研究这个问题的时候找到一个前辈写的依据输入法还是什么…来判断首字母的代码,忘了在哪了= =||工作中处理数据的时候,首字母是用excel来取,SQL导入数据库里的H的字符范围,按照这个代码的写法应该是
if($asc>=-17947 and $asc<=-17432)return "H";
查了半天表…发现没有找到I,这才想起来= =||
提供个连接 织梦做的,挺不错的
php 获取汉字的拼音(全拼音或者拼音首字母)
试过了 非常不错 。还附有一文本数据库
if($asc>=-17922 and $asc<=-17418)return "H";