Convert Chinese characters into pinyin code-PHP source code

Source: Internet
Author: User
The above three functions correspond to the Chinese character code, which is equivalent to the conversion to pinyin. Our example is simple. We only use the example code for converting Chinese characters starting with a to pinyin. The above three functions correspond to the Chinese character code, which is equivalent to the conversion to pinyin. Our example is simple. We only use the example code for converting Chinese characters starting with a to pinyin.

Script ec (2); script

$ Piny = array (
& Apos; a & apos; = & apos;-20319 & apos,
'Ai' =>-20317,
'Any' =>-20304,
'Ang '=>-20295
);
Echo getChineseSpells ('first Chinese WEB Station www.111cn.net ');
// Retrieve all Chinese Characters in pinyin
Function getChineseSpells ($ chinese, $ delimiter = '', $ first = 0)
{
$ Result = array ();
For ($ I = 0; $ I $ P = ord (substr ($ chinese, $ I, 1 ));
If ($ p & gt; 160 ){
$ Q = ord (substr ($ chinese, ++ $ I, 1 ));
$ P = $ p * 256 + $ q-65536;
}
$ Result [] = getChineseSpell ($ p );
If ($ first ){
Return $ result [0];
}
}
Return implode ($ delimiter, $ result );
}

// Obtain the pinyin corresponding to a Chinese character code
Function getChineseSpell ($ num, $ blank = ''){
If ($ num> 0 & $ num <160 ){
Return chr ($ num );
} Elseif ($ num <-20319 | $ num>-10247 ){
Return $ blank;
} Else {
Foreach (chineseSpellList as $ spell => $ code ){
If ($ code> $ num) break;
$ Result = $ spell;
}
Return $ result;
}
}

// Function, which is the first Chinese pinyin
Function getFirstSpell ($ chinese, $ length = 0 ){
$ Spell = getChineseSpells ($ chinese, '', 1 );
If ($ length ){
$ Spell = substr ($ spell, 0, $ length );
}
Return $ spell;
}
/*

*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.