Php Chinese language to pinyin function

Source: Internet
Author: User
Someone asked a question in Phpcms today about the php Chinese pinyin conversion function, that is, how PHP converts Chinese to pinyin. this is my first time seeing this question, so Baidu gave me a try, an available function is found. & lt ;? PhpfunctionPinyin ($ _ String, $ _ Code = 'utf8') {// php Chinese pinyin conversion function on the GBK page
Today, someone asked a question in Phpcms, that is, how PHP converts Chinese to pinyin. this is my first time seeing this question. so Baidu found a usable function.
    
 160) {$ _ Q = ord (substr ($ _ String, ++ $ I, 1 )); $ _ P = $ _ P * 256 + $ _ Q-65536;} $ _ Res. = _ Pinyin ($ _ P, $ _ Data);} return preg_replace ("/[^ a-z0-9] */", '', $ _ Res );} function _ Pinyin ($ _ Num, $ _ Data) {if ($ _ Num> 0 & $ _ Num <160) {return chr ($ _ Num );} elseif ($ _ Num <-20319 | $ _ Num>-10247) {return '';} else {foreach ($ _ Data as $ k => $ v) {if ($ v <= $ _ Num) break;} return $ k;} function _ U2_Utf8_Gb ($ _ C) {$ _ String = ''; If ($ _ C <0x80) {$ _ String. = $ _ C;} elseif ($ _ C <0x800) {$ _ String. = chr (0xC0 | $ _ C> 6); $ _ String. = chr (0x80 | $ _ C & 0x3F);} elseif ($ _ C <0x10000) {$ _ String. = chr (0xE0 | $ _ C> 12); $ _ String. = chr (0x80 | $ _ C> 6 & 0x3F); $ _ String. = chr (0x80 | $ _ C & 0x3F);} elseif ($ _ C <0x200000) {$ _ String. = chr (0xF0 | $ _ C> 18); $ _ String. = chr (0x80 | $ _ C> 12 & 0x3F); $ _ String. = chr (0x80 | $ _ C> 6 & 0x3F); $ _ Str Ing. = chr (0x80 | $ _ C & 0x3F);} return iconv ('utf-8', 'gb2312 ', $ _ String );} // Test echo Pinyin ('text in ', 'gb2312'); // The second parameter "1" can be set to utf8 encoding at will?>

The output is zhongwenzi.

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.