The method is to use a matrix. The Chinese character is composed of two ascii characters, one high-bit code and one low-bit code. The boundary is 128-264 64-128, respectively.
Each Chinese character Pinyin can contain a maximum of 8 characters, and thus form a two-dimensional matrix for query. The disadvantage is that it cannot solve the problem of polyphonic words.
Class pinyin {
/*
Whether to read the pinyin file to the memory, which consumes a little memory and hundreds of kb. The speed can be slightly improved,
*/
Var $ ismemorycache = 1;
/*
Retrieve only the first letter
*/
Var $ isfrist = 1;
/*
Pinyin matrix file address
*/
Var $ path = "py. qdb ";
/*
Memory pinyin matrix
*/
Var $ memorycache;
/*
Pinyin file handle
*/
Var $ handle;
/*
Conversion error box
*/
Var $ errormsgbox;
/*
Conversion result
*/
Var $ result;
Var $ array = array ();
Var $ n_t = array ("inline" => "a", "& aacute;" => "a", "inline" => "a", "& agrave; "=>" ","? "=>" ",
"Region" => "o", "& oacute;" => "o", "region" => "o", "& ograve;" => "o ",
"Identifier" => "e", "& eacute;" => "e", "identifier" => "e", "& egrave;" => "e ", "& ecirc;" => "e ",
"Identifier" => "I", "& iacute;" => "I", "identifier" => "I", "& igrave;" => "I ",
"Province" => "u", "& uacute;" => "u", "province" => "u", "& ugrave;" => "u ",
"Identifier" => "v", "& uuml; "=>" v"
);
/*
Conversion entry
@ Params $ str conversion characters, $ istonemark whether to retain the phonetic symbol $ suffix, the default is space
*/
Function chinesetopinyin ($ str, $ istonemark = 0, $ suffix = ""){
$ This-> py ($ str, $ istonemark, $ suffix );
Return $ this-> result;
}
Function get (){
Return $ this-> result;
}
Function py ($ str, $ n = 0, $ s = ""){
$ Strlength = strlen ($ str );
If ($ strlength = 0) {return "";}
$ This-> result = "";
If (is_array ($ str )){
Foreach ($ str as $ key => $ val ){
$ Str [$ key] = $ this-> py ($ val, $ n, $ s );
}
Return;
}
If (empty ($ this-> handle )){
If (! File_exists ($ this-> path )){
$ This-> addoneerrormsg (1, "the pinyin file path does not exist ");
Return false;
}
If (is_array ($ str )){
Foreach ($ str as $ key => $ val ){
$ Str [$ key] = $ this-> py ($ val, $ n, $ s );
}
}
If ($ this-> ismemorycache ){
If (! $ This-> memorycache ){
$ This-> memorycache = file_get_contents ($ this-> path );
For ($ I = 0; $ I <$ strlength; $ I ++ ){
$ Ord1 = ord (substr ($ str, $ I, 1 ));
'If ($ ord1> 128 ){
$ Ord2 = ord (substr ($ str, ++ $ I, 1 ));
If (! Isset ($ this-> array [$ ord1] [$ ord2]) {
$ Leng = ($ ord1-129) * (254-63) * 8 + 2) + ($ ord2-64) * 8;
$ This-> array [$ ord1] [$ ord2] = trim (substr ($ this-> memorycache, $ leng, 8 ));
}
$ Strtrlen = $ this-> isfrist? 1: 8;
$ This-> result. = substr ($ this-> array [$ ord1] [$ ord2], 0, $ strtrlen). $ s;
} Else {
$ This-> result. = substr ($ str, $ I, 1 );
}
}
}
} Else {
$ This-> handle = fopen ($ this-> path, "r ");
For ($ I = 0; $ I <$ strlength; $ I ++ ){
$ Ord1 = ord (substr ($ str, $ I, 1 ));
'If ($ ord1> 128 ){
$ Ord2 = ord (substr ($ str, ++ $ I, 1 ));
If (! Isset ($ this-> array [$ ord1] [$ ord2]) {
$ Leng = ($ ord1-129) * (254-63) * 8 + 2) + ($ ord2-64) * 8;
Fseek ($ this-> handle, $ leng );
$ This-> array [$ ord1] [$ ord2] = trim (fgets ($ this-> handle, 8 ));
}
$ Strtrlen = $ this-> isfrist? 1: 8;
$ This-> result. = substr ($ this-> array [$ ord1] [$ ord2], 0, $ strtrlen). $ s;
} Else {$ this-> result. = substr ($ str, $ I, 1 );}
}
}
If (! $ N) {$ this-> result = strtr ($ this-> result, $ this-> n_t );}
}
}
Function addoneerrormsg ($ no, $ reason ){
$ This-> errormsgbox [] = "<B> error: </B>". $ no. ",". $ reason;
}
Function showerrormsg (){
Foreach ($ this-> errormsgbox as $ val ){
Echo $ val. "rnrn </br> ";
}
}
Function _ destruct (){
If (is_array ($ this-> errormsgbox )){
$ This-> showerrormsg ();
}
}
}
I have encountered this problem before and found that the code that has been circulated is not perfect. The Chinese character Library has a total of 20 k + Chinese characters, most of which are intended to be fooled by hundreds of commonly used Chinese characters, today, it's impossible to get around in Mars.
There is also a way to read the dictionary and convert it. Each line has one Chinese character | pinyin. This disadvantage is very high, slow, and consumes a lot of memory. Just read the array in explode and recycle it once, it can consume hundreds of MB of memory. If a single page consumes hundreds of MB, the load will be slightly larger and it will only be able to shed tears.