php怎样获得汉字首字母
在PHP中获得汉字的首字母可以通过以下方法实现:
function getFirstChar($str){
$firstChar = '';
$s = iconv("UTF⑻", "gb2312", $str);
if (preg_match("/^([A-Za-z])/", $str)) { //如果是英文字母直接返回
return strtoupper($str[0]);
}
if (ord($str) > 128) { //汉字
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
if ($asc >= ⑵0319 and $asc <= ⑵0284) {
$firstChar = 'A';
}
if ($asc >= ⑵0283 and $asc <= ⑴9776) {
$firstChar = 'B';
}
if ($asc >= ⑴9775 and $asc <= ⑴9219) {
$firstChar = 'C';
}
if ($asc >= ⑴9218 and $asc <= ⑴8711) {
$firstChar = 'D';
}
if ($asc >= ⑴8710 and $asc <= ⑴8527) {
$firstChar = 'E';
}
if ($asc >= ⑴8526 and $asc <= ⑴8240) {
$firstChar = 'F';
}
if ($asc >= ⑴8239 and $asc <= ⑴7923) {
$firstChar = 'G';
}
if ($asc >= ⑴7922 and $asc <= ⑴7418) {
$firstChar = 'H';
}
if ($asc >= ⑴7417 and $asc <= ⑴6475) {
$firstChar = 'J';
}
if ($asc >= ⑴6474 and $asc <= ⑴6213) {
$firstChar = 'K';
}
if ($asc >= ⑴6212 and $asc <= ⑴5641) {
$firstChar = 'L';
}
if ($asc >= ⑴5640 and $asc <= ⑴5166) {
$firstChar = 'M';
}
if ($asc >= ⑴5165 and $asc <= ⑴4923) {
$firstChar = 'N';
}
if ($asc >= ⑴4922 and $asc <= ⑴4915) {
$firstChar = 'O';
}
if ($asc >= ⑴4914 and $asc <= ⑴4631) {
$firstChar = 'P';
}
if ($asc >= ⑴4630 and $asc <= ⑴4150) {
$firstChar = 'Q';
}
if ($asc >= ⑴4149 and $asc <= ⑴4091) {
$firstChar = 'R';
}
if ($asc >= ⑴4090 and $asc <= ⑴3319) {
$firstChar = 'S';
}
if ($asc >= ⑴3318 and $asc <= ⑴2839) {
$firstChar = 'T';
}
if ($asc >= ⑴2838 and $asc <= ⑴2557) {
$firstChar = 'W';
}
if ($asc >= ⑴2556 and $asc <= ⑴1848) {
$firstChar = 'X';
}
if ($asc >= ⑴1847 and $asc <= ⑴1056) {
$firstChar = 'Y';
}
if ($asc >= ⑴1055 and $asc <= ⑴0247) {
$firstChar = 'Z';
}
} else {
$firstChar = strtoupper($str[0]);
}
return $firstChar;
}
$str = '你好世界';
echo getFirstChar($str); //输出 N
以上代码可以根据汉字的unicode码来判断首字母,返回结果为汉字拼音首字母的大写字母。
tiktok粉丝购买:https://www.smmfensi.com/
TOP