汉字转拼音首字母缩写
1 <?php 2 3 class PinYinGenerator 4 { 5 6 private static function getfirstchar($s0){ 7 8 $fchar = ord($s0{0}); 9 if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0}); 10 try 11 { 12 $s1 = iconv("UTF-8","gb2312", $s0); 13 $s2 = iconv("gb2312","UTF-8", $s1); 14 if($s2 == $s0){$s = $s1;}else{$s = $s0;} 15 $asc = ord($s{0}) * 256 + ord($s{1}) - 65536; 16 17 18 19 if($asc >= -20319 and $asc <= -20284) return "A"; 20 if($asc >= -20283 and $asc <= -19776) return "B"; 21 if($asc >= -19775 and $asc <= -19219) return "C"; 22 if($asc >= -19218 and $asc <= -18711) return "D"; 23 if($asc >= -18710 and $asc <= -18527) return "E"; 24 if($asc >= -18526 and $asc <= -18240) return "F"; 25 if($asc >= -18239 and $asc <= -17923) return "G"; 26 if($asc >= -17922 and $asc <= -17418) return "H"; 27 if($asc >= -17417 and $asc <= -16475) return "J"; 28 if($asc >= -16474 and $asc <= -16213) return "K"; 29 if($asc >= -16212 and $asc <= -15641) return "L"; 30 if($asc >= -15640 and $asc <= -15166) return "M"; 31 if($asc >= -15165 and $asc <= -14923) return "N"; 32 if($asc >= -14922 and $asc <= -14915) return "O"; 33 if($asc >= -14914 and $asc <= -14631) return "P"; 34 if($asc >= -14630 and $asc <= -14150) return "Q"; 35 if($asc >= -14149 and $asc <= -14091) return "R"; 36 if($asc >= -14090 and $asc <= -13319) return "S"; 37 if($asc >= -13318 and $asc <= -12839) return "T"; 38 if($asc >= -12838 and $asc <= -12557) return "W"; 39 if($asc >= -12556 and $asc <= -11848) return "X"; 40 if($asc >= -11847 and $asc <= -11056) return "Y"; 41 if($asc >= -11055 and $asc <= -10247) return "Z"; 42 return null; 43 } 44 catch(Exception $ex) 45 { 46 throw new $ex; 47 } 48 } 49 50 51 public static function firstPinYin($zh){ 52 try 53 { 54 $ret = ""; 55 $s1 = iconv("UTF-8","gb2312", $zh); 56 $s2 = iconv("gb2312","UTF-8", $s1); 57 if($s2 == $zh){$zh = $s1;} 58 for($i = 0; $i < strlen($zh); $i++){ 59 $s1 = substr($zh,$i,1); 60 $p = ord($s1); 61 if($p > 160){ 62 $s2 = substr($zh,$i++,2); 63 $ret .= self::getfirstchar($s2); 64 }else{ 65 $ret .= $s1; 66 } 67 } 68 return $ret; 69 70 } 71 catch(Exception $ex) 72 { 73 throw $ex; 74 } 75 76 } 77 } 78 ?> 79 //调用方式: 80 $newPinYin=PinYinGenerator::firstPinYin('汉字字符串或其他都行,只不过不是汉字的不给转罢了'); 81 echo $newPinYin 82 输出结果:HZZFCHQTDXZBGBSHZDBGZBL
因为陌生所以勇敢~