GBK to pinyin. Note that GB2312 to pinyin is not everywhere on the network.

Source: Internet
Author: User
GBK to pinyin. Note that GB2312 to pinyin is not everywhere on the network.
A search on the internet is full of "PHP to pinyin", but the code only supports GB2312,
Even the words "Taobao", "Taobao", and "Hui" cannot be supported.
The code for converting Chinese names into pinyin is far from enough.

Now I am releasing a correct GBK-to-Pinyin code. thanks to the author: Ma Minglian (! Hightman home: http://php.twomice.net
The original DEMO only supports one word query. I changed it to a string that can be used in both Chinese and English. The original text is returned in English, and the Chinese characters are converted to pinyin with tones.
At present, it does not support polyphonic words. for example, "Zeng" will be converted to ceng2 without zeng1.

(There is a yii-chinese PHP project on the network. Unfortunately, the conversion to pinyin is also GB2312)


Usage:
$ Str = 'Chinese characters abc123-= + ';
$ Py = pinyin: instance (); // single instance. if you want to, $ py = new pinyin.
Echo $ py-> get ($ str );


Download the entire code at the end of the page.


  1. Class pinyin {
  2. Var $ _ fp = false;
  3. Function _ construct (){
  4. $ _ Dat = DISCUZ_ROOT. "./source/include/table/py. dat ";
  5. If (! $ This-> _ fp)
  6. $ This-> _ fp = fopen ($ _ dat, 'RB ');
  7. }
  8. /**
  9. * Return a simple instance
  10. * @ Return
  11. */
  12. Function & instance (){
  13. Static $ object;
  14. If (empty ($ object )){
  15. $ Object = new self ();
  16. }
  17. Return $ object;
  18. }
  19. Function anystring2gbk ($ str ){
  20. $ Encode = mb_detect_encoding ($ str, "ASCII, UNICODE, UTF-8, GBK, CP936, EUC-CN, BIG-5, EUC-TW ");
  21. Return ($ encode! = 'Cp936' & $ encode! = 'Ascii '& $ encode! = 'Gbk '? Iconv ($ encode, 'gbk', $ str): $ str );
  22. }
  23. Function get ($ str, $ separator = ''){
  24. // $ Str = iconv ('utf-8', 'gbk', $ str );
  25. $ Str = anystring2gbk ($ str); // If you can confirm the input encoding, you can use the previous line of code.
  26. $ Len = strlen ($ str );
  27. $ I = 0; $ result = array ();
  28. While ($ I <$ len ){
  29. $ S = ord ($ str {$ I });
  30. If ($ s & gt; 160 ){
  31. $ Word = $ this-> word2py ($ s, ord ($ str {++ $ I }));
  32. } Else {
  33. $ Word = $ str {$ I };
  34. }
  35. $ Result [] = $ word;
  36. ++ $ I;
  37. }
  38. Return implode ($ separator, $ result );
  39. }
  40. Private function word2py ($ h, $ l ){
  41. $ High = $ h-0x81;
  42. $ Low = $ l-0x40;
  43. // Calculate the offset
  44. $ Off = ($ high <8) + $ low-($ high * 0x40 );
  45. // Determine the off value
  46. If ($ off <0 ){
  47. Return chr ($ h). chr ($ l );
  48. }
  49. Fseek ($ this-> _ fp, $ off * 8, SEEK_SET );
  50. $ Ret = fread ($ this-> _ fp, 8 );
  51. $ Ret = unpack ('a8py', $ ret );
  52. Return $ ret ['py'];
  53. }
  54. Function _ destruct (){
  55. If ($ this-> _ fp)
  56. Fclose ($ this-> _ fp );
  57. }
  58. }
  59. ?>

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.