• 技术文章 >php教程 >php手册

    php获取中文字符拼音首字母实例

    2016-05-25 16:44:24原创443
    在php中如果我们要获取汉字的拼音第一个字母我们先要了解asc码的范围码了,我们只要知道这个值上下限就可以使用php中的ord来获取我相关的汉字拼音了.

    实例1,代码如下:

    function getFirstCharter($str) { 
        if (emptyempty($str)) {return '';} 
        $fchar = ord($str{0}); 
        if ($fchar>=ord('A') && $fchar<=ord('z')) return strtoupper($str{0}); 
        $s1 = iconv('UTF-8', 'gb2312', $str); 
        $s2 = iconv('gb2312', 'UTF-8', $s1); 
        $s = $s2 == $str ? $s1 : $str; 
        $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'; 
        return null; 
    }

    例如:echo getFirstCharter("程序员3aj.cn"); // 结果将输出:C

    实例二,代码如下:

    "; 
    $i=0; 
    while($i='B0'){ //汉字的开始 
            $t=getLetter(hexdec(bin2hex(substr($str,$i,2)))); 
            printf("%c",$t==-1 ? '*' : $t ); 
            $i+=2; 
        } 
        else{ 
            printf("%s",substr($str,$i,1)); 
            $i++; 
        } 
    } 
     
    function getLetter($num){ 
        global $limit; 
        $char_index=65; 
        foreach($limit as $k=>$v){ 
            if($num>=$v[0] && $num<=$v[1]){ 
                $char_index+=$k; 
                return $char_index; 
            } 
        } 
        return -1; 
    }


    教程网址:

    欢迎收藏∩_∩但请保留本文链接。

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php获取 中文字符 首字母
    上一篇:php批量修改windows目录权限程序 下一篇:php Exception打印error trace 实例
    PHP编程就业班

    相关文章推荐

    • 无限级别菜单的实现(其实还是有限级别的^0^)• PHP session信息存储到数据库的类• php页面跳转的几种实现方法• PHP5.5迭代生成器用法实例详解,php5.5生成器• php使用curl获取https请求的方法

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网