廢話不多說直接上代碼
復制代碼 代碼如下:
<?php
class GetPingYing {
private $pylist = array(
a
=>
ai
=>
an
=>
ang
=>
ao
=>
ba
=>
bai
=>
ban
=>
bang
=>
bao
=>
bei
=>
ben
=>
beng
=>
bi
=>
bian
=>
biao
=>
bie
=>
bin
=>
bing
=>
bo
=>
bu
=>
ca
=>
cai
=>
can
=>
cang
=>
cao
=>
ce
=>
ceng
=>
cha
=>
chai
=>
chan
=>
chang
=>
chao
=>
che
=>
chen
=>
cheng
=>
chi
=>
chong
=>
chou
=>
chu
=>
chuai
=>
chuan
=>
chuang
=>
chui
=>
chun
=>
chuo
=>
ci
=>
cong
=>
cou
=>
cu
=>
cuan
=>
cui
=>
cun
=>
cuo
=>
da
=>
dai
=>
dan
=>
dang
=>
dao
=>
de
=>
deng
=>
di
=>
dian
=>
diao
=>
die
=>
ding
=>
diu
=>
dong
=>
dou
=>
du
=>
duan
=>
dui
=>
dun
=>
duo
=>
e
=>
en
=>
er
=>
fa
=>
fan
=>
fang
=>
fei
=>
fen
=>
feng
=>
fo
=>
fou
=>
fu
=>
ga
=>
gai
=>
gan
=>
gang
=>
gao
=>
ge
=>
gei
=>
gen
=>
geng
=>
gong
=>
gou
=>
gu
=>
gua
=>
guai
=>
guan
=>
guang
=>
gui
=>
gun
=>
guo
=>
ha
=>
hai
=>
han
=>
hang
=>
hao
=>
he
=>
hei
=>
hen
=>
heng
=>
hong
=>
hou
=>
hu
=>
hua
=>
huai
=>
huan
=>
huang
=>
hui
=>
hun
=>
huo
=>
ji
=>
jia
=>
jian
=>
jiang
=>
jiao
=>
jie
=>
jin
=>
jing
=>
jiong
=>
jiu
=>
ju
=>
juan
=>
jue
=>
jun
=>
ka
=>
kai
=>
kan
=>
kang
=>
kao
=>
ke
=>
ken
=>
keng
=>
kong
=>
kou
=>
ku
=>
kua
=>
kuai
=>
kuan
=>
kuang
=>
kui
=>
kun
=>
kuo
=>
la
=>
lai
=>
lan
=>
lang
=>
lao
=>
le
=>
lei
=>
leng
=>
li
=>
lia
=>
lian
=>
liang
=>
liao
=>
lie
=>
lin
=>
ling
=>
liu
=>
long
=>
lou
=>
lu
=>
lv
=>
luan
=>
lue
=>
lun
=>
luo
=>
ma
=>
mai
=>
man
=>
mang
=>
mao
=>
me
=>
mei
=>
men
=>
meng
=>
mi
=>
mian
=>
miao
=>
mie
=>
min
=>
ming
=>
miu
=>
mo
=>
mou
=>
mu
=>
na
=>
nai
=>
nan
=>
nang
=>
nao
=>
ne
=>
nei
=>
nen
=>
neng
=>
ni
=>
nian
=>
niang
=>
niao
=>
nie
=>
nin
=>
ning
=>
niu
=>
nong
=>
nu
=>
nv
=>
nuan
=>
nue
=>
nuo
=>
o
=>
ou
=>
pa
=>
pai
=>
pan
=>
pang
=>
pao
=>
pei
=>
pen
=>
peng
=>
pi
=>
pian
=>
piao
=>
pie
=>
pin
=>
ping
=>
po
=>
pu
=>
qi
=>
qia
=>
qian
=>
qiang
=>
qiao
=>
qie
=>
qin
=>
qing
=>
qiong
=>
qiu
=>
qu
=>
quan
=>
que
=>
qun
=>
ran
=>
rang
=>
rao
=>
re
=>
ren
=>
reng
=>
ri
=>
rong
=>
rou
=>
ru
=>
ruan
=>
rui
=>
run
=>
ruo
=>
sa
=>
sai
=>
san
=>
sang
=>
sao
=>
se
=>
sen
=>
seng
=>
sha
=>
shai
=>
shan
=>
shang
=>
shao
=>
she
=>
shen
=>
sheng
=>
shi
=>
shou
=>
shu
=>
shua
=>
shuai
=>
shuan
=>
shuang
=>
shui
=>
shun
=>
shuo
=>
si
=>
song
=>
sou
=>
su
=>
suan
=>
sui
=>
sun
=>
suo
=>
ta
=>
tai
=>
tan
=>
tang
=>
tao
=>
te
=>
teng
=>
ti
=>
tian
=>
tiao
=>
tie
=>
ting
=>
tong
=>
tou
=>
tu
=>
tuan
=>
tui
=>
tun
=>
tuo
=>
wa
=>
wai
=>
wan
=>
wang
=>
wei
=>
wen
=>
weng
=>
wo
=>
wu
=>
xi
=>
xia
=>
xian
=>
xiang
=>
xiao
=>
xie
=>
xin
=>
xing
=>
xiong
=>
xiu
=>
xu
=>
xuan
=>
xue
=>
xun
=>
ya
=>
yan
=>
yang
=>
yao
=>
ye
=>
yi
=>
yin
=>
ying
=>
yo
=>
yong
=>
you
=>
yu
=>
yuan
=>
yue
=>
yun
=>
za
=>
zai
=>
zan
=>
zang
=>
zao
=>
ze
=>
zei
=>
zen
=>
zeng
=>
zha
=>
zhai
=>
zhan
=>
zhang
=>
zhao
=>
zhe
=>
zhen
=>
zheng
=>
zhi
=>
zhong
=>
zhou
=>
zhu
=>
zhua
=>
zhuai
=>
zhuan
=>
zhuang
=>
zhui
=>
zhun
=>
zhuo
=>
zi
=>
zong
=>
zou
=>
zu
=>
zuan
=>
zui
=>
zun
=>
zuo
=>
);
//全部拼音
public function getAllPY($chinese
$delimiter =
$length =
) {
$py = $this
>zh_to_pys($chinese
$delimiter);
if($length) {
$py = substr($py
$length);
}
return $py;
}
//拼音首個字母
public function getFirstPY($chinese){
$result =
;
for ($i=
; $i<strlen($chinese); $i++) {
$p = ord(substr($chinese
$i
));
if ($p>
) {
$q = ord(substr($chinese
++$i
));
$p = $p*
+ $q
;
}
$result
= substr($this
>zh_to_py($p)
);
}
return $result ;
}
//
中文轉拼音
//
private function zh_to_py($num
$blank =
) {
if($num>
&& $num<
) {
return chr($num);
} elseif ($num<
||$num>
) {
return $blank;
} else {
foreach ($this
>pylist as $py => $code) {
if($code > $num) break;
$result = $py;
}
return $result;
}
}
private function zh_to_pys($chinese
$delimiter =
$first=
){
$result = array();
for($i=
; $i<strlen($chinese); $i++) {
$p = ord(substr($chinese
$i
));
if($p>
) {
$q = ord(substr($chinese
++$i
));
$p = $p*
+ $q
;
}
$result[] = $this
>zh_to_py($p);
if ($first) {
return $result[
];
}
}
return implode($delimiter
$result);
}
}
//
中文轉拼音結束
//
//中文是雙字節
所以需要兩個字節連接起來(ASCII碼的范圍是在
)
$c =
齊秦
;
for($i=
; $i<strlen($c); $i++) {
echo ord($c[$i])
;//
}
echo
<br>
chr(
)
chr(
)
chr(
)
chr(
)
<br>
;
//測試
$PingYing = new GetPingYing();
echo
<br>
$PingYing
>getFirstPY(
羽泉樂隊
歌不錯
推薦
)
<br>
;//yqldgbc
tj
echo $PingYing
>getAllPY(
羽泉樂隊
歌不錯
推薦
)
<br>
; //yuquanleduigebucuo
tuijian
?>
From:http://tw.wingwit.com/Article/program/PHP/201311/21313.html