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