获取多个汉字首字母(php)

 1 <?php  2 function getfirstchar($s0){  3     $fchar = ord($s0{0});  4     if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});  5     $s1 = iconv("UTF-8","gb2312", $s0);  6     $s2 = iconv("gb2312","UTF-8", $s1);  7     if($s2 == $s0){$s = $s1;}else{$s = $s0;}  8     $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;  9     if($asc >= -20319 and $asc <= -20284) return "A"; 10     if($asc >= -20283 and $asc <= -19776) return "B"; 11     if($asc >= -19775 and $asc <= -19219) return "C"; 12     if($asc >= -19218 and $asc <= -18711) return "D"; 13     if($asc >= -18710 and $asc <= -18527) return "E"; 14     if($asc >= -18526 and $asc <= -18240) return "F"; 15     if($asc >= -18239 and $asc <= -17923) return "G"; 16     if($asc >= -17922 and $asc <= -17418) return "I"; 17     if($asc >= -17417 and $asc <= -16475) return "J"; 18     if($asc >= -16474 and $asc <= -16213) return "K"; 19     if($asc >= -16212 and $asc <= -15641) return "L"; 20     if($asc >= -15640 and $asc <= -15166) return "M"; 21     if($asc >= -15165 and $asc <= -14923) return "N"; 22     if($asc >= -14922 and $asc <= -14915) return "O"; 23     if($asc >= -14914 and $asc <= -14631) return "P"; 24     if($asc >= -14630 and $asc <= -14150) return "Q"; 25     if($asc >= -14149 and $asc <= -14091) return "R"; 26     if($asc >= -14090 and $asc <= -13319) return "S"; 27     if($asc >= -13318 and $asc <= -12839) return "T"; 28     if($asc >= -12838 and $asc <= -12557) return "W"; 29     if($asc >= -12556 and $asc <= -11848) return "X"; 30     if($asc >= -11847 and $asc <= -11056) return "Y"; 31     if($asc >= -11055 and $asc <= -10247) return "Z"; 32     return null; 33 } 34  

35  

36 function pinyin1($zh){ 37     $ret = ""; 38     $s1 = iconv("UTF-8","gb2312", $zh); 39     $s2 = iconv("gb2312","UTF-8", $s1); 40     if($s2 == $zh){$zh = $s1;} 41     for($i = 0; $i < strlen($zh); $i++){ 42         $s1 = substr($zh,$i,1); 43         $p = ord($s1); 44         if($p > 160){ 45             $s2 = substr($zh,$i++,2); 46             $ret .= getfirstchar($s2); 47         }else{ 48             $ret .= $s1; 49  } 50  } 51     return $ret; 52 } 53 echo "北京信息学校<br/>"; 54 echo pinyin1('北京信息学校'); 

    感谢网友分享

你可能感兴趣的:(PHP)