function getFirstChar($s) {
//获取第一个
$s0 = mb_substr($s, 0, 1, 'utf-8');
//一些汉字可能获取不到,可以如下单独处理
if ($s0 == '奧') return 'A';
if ($s0 == '斐') return 'F';
if ($s0 == '麒') return 'Q';
if ($s0 == '敘') return 'X';
//将UTF-8转换成GB2312编码
$s = iconv('UTF-8','GBK', $s0);
//汉字开头,汉字没有以U、V开头的
if (ord($s0) > 128) {
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
if($asc >= -20319 && $asc <= -20284) return 'A';
if($asc >= -20283 && $asc <= -19776) return 'B';
if($asc >= -19775 && $asc <= -19219) return 'C';
if($asc >= -19218 && $asc <= -18711) return 'D';
if($asc >= -18710 && $asc <= -18527) return 'E';
if($asc >= -18526 && $asc <= -18240) return 'F';
if($asc >= -18239 && $asc <= -17923) return 'G';
if($asc >= -17922 && $asc <= -17418) return 'H';
if($asc >= -17417 && $asc <= -16475) return 'J';
if($asc >= -16474 && $asc <= -16213) return 'K';
if($asc >= -16212 && $asc <= -15641) return 'L';
if($asc >= -15640 && $asc <= -15166) return 'M';
if($asc >= -15165 && $asc <= -14923) return 'N';
if($asc >= -14922 && $asc <= -14915) return 'O';
if($asc >= -14914 && $asc <= -14631) return 'P';
if($asc >= -14630 && $asc <= -14150) return 'Q';
if($asc >= -14149 && $asc <= -14091) return 'R';
if($asc >= -14090 && $asc <= -13319) return 'S';
if($asc >= -13318 && $asc <= -12839) return 'T';
if($asc >= -12838 && $asc <= -12557) return 'W';
if($asc >= -12556 && $asc <= -11848) return 'X';
if($asc >= -11847 && $asc <= -11056) return 'Y';
if($asc >= -11055 && $asc <= -10247) return 'Z';
} elseif (ord($s) >= 48 && ord($s) <= 57) {//数字开头
switch(iconv_substr($s, 0, 1, 'utf-8')) {
case 1:return 'Y';
case 2:return 'E';
case 3:return 'S';
case 4:return 'S';
case 5:return 'W';
case 6:return 'L';
case 7:return 'Q';
case 8:return 'B';
case 9:return 'J';
case 0:return 'L';
}
} elseif (ord($s) >= 65 && ord($s) <= 90) {//大写英文开头
return substr($s,0,1);
} elseif (ord($s) >= 97 && ord($s) <= 122) {//小写英文开头
return strtoupper(substr($s,0,1));
} else {//中英混合的词语提取首个字符即可
return iconv_substr($s0, 0, 1, 'utf-8');
}
return '';
}
//调用
getFirstChar('我是最棒的!'); //W
需要获取小写的就把代码里的大写字母改为小写的