欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php 中文字符串首字母的获取函数分享

程序员文章站 2022-06-11 15:46:41
复制代码 代码如下:function chinesefirst($str){    $str= iconv("utf-8","gb2312",...
复制代码 代码如下:

function chinesefirst($str)
{

    $str= iconv("utf-8","gb2312", $str);    //如果程序是gbk的,此行就要注释掉

    //判断字符串是否全都是中文
    if (preg_match("/^[\x7f-\xff]/", $str))
    {
        $fchar=ord($str{0});  
        if($fchar>=ord("a") and $fchar<=ord("z") )return strtoupper($str{0});
        $a = $str;
        $val=ord($a{0})*256+ord($a{1})-65536;
        if($val>=-20319 and $val<=-20284)return "a";  
        if($val>=-20283 and $val<=-19776)return "b";  
        if($val>=-19775 and $val<=-19219)return "c";  
        if($val>=-19218 and $val<=-18711)return "d";  
        if($val>=-18710 and $val<=-18527)return "e";  
        if($val>=-18526 and $val<=-18240)return "f";  
        if($val>=-18239 and $val<=-17923)return "g";  
        if($val>=-17922 and $val<=-17418)return "h";
        if($val>=-17417 and $val<=-16475)return "j";                
        if($val>=-16474 and $val<=-16213)return "k";                
        if($val>=-16212 and $val<=-15641)return "l";                
        if($val>=-15640 and $val<=-15166)return "m";                
        if($val>=-15165 and $val<=-14923)return "n";                
        if($val>=-14922 and $val<=-14915)return "o";                
        if($val>=-14914 and $val<=-14631)return "p";                
        if($val>=-14630 and $val<=-14150)return "q";                
        if($val>=-14149 and $val<=-14091)return "r";                
        if($val>=-14090 and $val<=-13319)return "s";                
        if($val>=-13318 and $val<=-12839)return "t";                
        if($val>=-12838 and $val<=-12557)return "w";                
        if($val>=-12556 and $val<=-11848)return "x";                
        if($val>=-11847 and $val<=-11056)return "y";                
        if($val>=-11055 and $val<=-10247)return "z";
    } else
    {
        return false;
    }

}