<?php
$pinyins = Array ();
When the phonetic ishead=0 of a string of Chinese characters is obtained, the output pinyin first letter when outputting the whole phonetic ishead=1
function Getpinyin ($str, $ishead =0, $isclose =1) {
Global $pinyins;
$restr = "";
$str = Trim ($STR);
$slen = strlen ($STR);
if ($slen <2) return $str;
if (count ($pinyins) ==0) {
$fp = fopen ("./pinyin.db", "R");
while (!feof ($fp)) {
$line = Trim (fgets ($fp));
$pinyins [$line [0]. $line [1]] = substr ($line, 3,strlen ($line)-3);
}
Fclose ($FP);
}
for ($i =0; $i < $slen; $i + +) {
if (Ord ($str [$i]) >0x80)
{
$c = $str [$i]. $str [$i +1];
$i + +;
if (Isset ($pinyins [$c])) {
if ($ishead ==0) $restr. = $pinyins [$c];
else $restr. = $pinyins [$c][0];
}else $restr. = "_";
}else if (eregi ("[a-z0-9]", $str [$i])) {$restr. = $str [$i];}
else{$restr. = "_"; }
}
if ($isclose ==0) unset ($pinyins);
return $restr;
}
if ($_get[' Hanzi '])
{
echo $pinyin =getpinyin ($_get[' Hanzi '],1,0);
echo "obtained pinyin is:". $pinyin;
}
?>