menu
more_vert
php 获取每个汉字的拼音首字母

作者:dayu

技术分享
 1 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 2 <?php
 3 function getfirstchar($s0){
 4     $fchar = ord($s0{0});
 5     if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
 6     $s1 = $s0;
 7     $s2 = $s1;
 8     //$s1 = iconv("UTF-8","gb2312", $s0);
 9     //$s2 = iconv("gb2312","UTF-8", $s1);
10     if($s2 == $s0){$s = $s1;}else{$s = $s0;}
11     $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
12     if($asc >= -20319 and $asc <= -20284) return "A";
13     if($asc >= -20283 and $asc <= -19776) return "B";
14     if($asc >= -19775 and $asc <= -19219) return "C";
15     if($asc >= -19218 and $asc <= -18711) return "D";
16     if($asc >= -18710 and $asc <= -18527) return "E";
17     if($asc >= -18526 and $asc <= -18240) return "F";
18     if($asc >= -18239 and $asc <= -17923) return "G";
19     if($asc >= -17922 and $asc <= -17418) return "I";
20     if($asc >= -17417 and $asc <= -16475) return "J";
21     if($asc >= -16474 and $asc <= -16213) return "K";
22     if($asc >= -16212 and $asc <= -15641) return "L";
23     if($asc >= -15640 and $asc <= -15166) return "M";
24     if($asc >= -15165 and $asc <= -14923) return "N";
25     if($asc >= -14922 and $asc <= -14915) return "O";
26     if($asc >= -14914 and $asc <= -14631) return "P";
27     if($asc >= -14630 and $asc <= -14150) return "Q";
28     if($asc >= -14149 and $asc <= -14091) return "R";
29     if($asc >= -14090 and $asc <= -13319) return "S";
30     if($asc >= -13318 and $asc <= -12839) return "T";
31     if($asc >= -12838 and $asc <= -12557) return "W";
32     if($asc >= -12556 and $asc <= -11848) return "X";
33     if($asc >= -11847 and $asc <= -11056) return "Y";
34     if($asc >= -11055 and $asc <= -10247) return "Z";
35     return null;
36 }
37 
38 
39 function pinyin1($zh){
40     $ret = "";
41     $s1 = iconv("UTF-8","gb2312", $zh);
42     $s2 = iconv("gb2312","UTF-8", $s1);
43     if($s2 == $zh){$zh = $s1;}
44     for($i = 0; $i < strlen($zh); $i++){
45         $s1 = substr($zh,$i,1);
46         $p = ord($s1);
47         if($p > 160){
48             $s2 = substr($zh,$i++,2);
49             $ret .= getfirstchar($s2);
50         }else{
51             $ret .= $s1;
52         }
53     }
54     return $ret;
55 }
56 echo "测试123aaa<br/>";
57 echo pinyin1(‘测试123aaa‘);
58 
59 ?>

View Code

php 获取每个汉字的拼音首字母

原文地址:http://www.cnblogs.com/zheart/p/7239329.html