Home >Backend Development >PHP Tutorial >获取汉字拼音首字母函数的疑问

获取汉字拼音首字母函数的疑问

WBOY
WBOYOriginal
2016-06-23 13:13:201024browse

public function getinitial($str){ 
    $asc=ord(substr($str,0,1)); 
    if ($asc        if ($asc>=48 && $asc            return '1'; //数字 
        }elseif ($asc>=65 && $asc            return chr($asc); // A--Z 
        }elseif ($asc>=97 && $asc            return chr($asc-32); // a--z 
        }else{ 
            return '~'; //其他 
        } 
    }else{ //中文
            $asc=$asc*1000+ord(substr($str,1,1)); 
            //获取拼音首字母A--Z 
            if ($asc>=176161 && $asc                return 'A'; 
            }elseif ($asc>=176197 && $asc                return 'B'; 
            }elseif ($asc>=178193 && $asc                return 'C'; 
            }elseif ($asc>=180238 && $asc                return 'D'; 
            }elseif ($asc>=182234 && $asc                return 'E'; 
            }elseif ($asc>=183162 && $asc                return 'F'; 
            }elseif ($asc>=184193 && $asc                return 'G'; 
            }elseif ($asc>=185254 && $asc                return 'H'; 
            }elseif ($asc>=187247 && $asc                return 'J'; 
            }elseif ($asc>=191166 && $asc                return 'K'; 
            }elseif ($asc>=192172 && $asc                return 'L'; 
            }elseif ($asc>=194232 && $asc                return 'M'; 
            }elseif ($asc>=196195 && $asc                return 'N'; 
            }elseif ($asc>=197182 && $asc                return 'O'; 
            }elseif ($asc>=197190 && $asc                return 'P'; 
            }elseif ($asc>=198218 && $asc                return 'Q'; 
            }elseif ($asc>=200187 && $asc                return 'R'; 
            }elseif ($asc>=200246 && $asc                return 'S'; 
            }elseif ($asc>=203250 && $asc                return 'T'; 
            }elseif ($asc>=205218 && $asc                return 'W'; 
            }elseif ($asc>=206244 && $asc                return 'X'; 
            }elseif ($asc>=209185 && $asc                return 'Y'; 
            }elseif ($asc>=212209){ 
                return 'Z'; 
            }else{ 
                return '~'; 
            } 
        } 
    } 

这些176161 、176197怎么得来的,求大神指点


回复讨论(解决方案)

你这个是根据 gb2312 一级字库 得来的
gb2312 分为 一级字库(按汉语拼音首字母排列)和 二级字库(按偏旁部首排列)

gb2312 编码由两字节构成,称为区位码

你这个是根据 gb2312 一级字库 得来的
gb2312 分为 一级字库(按汉语拼音首字母排列)和 二级字库(按偏旁部首排列)

gb2312 编码由两字节构成,称为区位码


谢谢
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