pinyin.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * @name Pinyin_Pinyin
  4. * @desc 汉字转拼音 (utf8)
  5. * @author 张顺(zhangshun@baidu.com)
  6. */
  7. include 'ChinesePinyinTable.php';
  8. class Pinyin_Pinyin {
  9. /**
  10. * @desc split string
  11. * @param string $string
  12. * @return array
  13. **/
  14. private function splitString($string) {
  15. $arrResult = array();
  16. $intLen = mb_strlen($string);
  17. while ($intLen) {
  18. $arrResult[] = mb_substr($string, 0, 1, 'utf8');
  19. $string = mb_substr($string, 1, $intLen, 'utf8');
  20. $intLen = mb_strlen($string);
  21. }
  22. return $arrResult;
  23. }
  24. /**
  25. * @desc change to single character list to pinyin list
  26. * @param array $arrStringList
  27. * @return array
  28. **/
  29. private function toPinyinList($arrStringList) {
  30. $arrResult = array();
  31. if (!is_array($arrStringList)) {
  32. return $arrResult;
  33. }
  34. foreach ($arrStringList as $string) {
  35. switch (strlen($string)) {
  36. case 1:
  37. $arrResult[] = array($string);
  38. break;
  39. case 3:
  40. if (isset(Pinyin_ChinesePinyinTable::$arrChinesePinyinTable[$string])) {
  41. $arrResult[] =
  42. Pinyin_ChinesePinyinTable::$arrChinesePinyinTable[$string];
  43. } else {
  44. $arrResult[] = array($string);
  45. }
  46. break;
  47. default :
  48. $arrResult[] = array($string);
  49. }
  50. }
  51. return $arrResult;
  52. }
  53. /**
  54. * @desc convert chinese(include letter & number) to pinyin
  55. * @param string $string
  56. * @param boolean $isSimple
  57. * @param boolean $isInitial
  58. * @param boolean $isPolyphone
  59. * @param boolean $isAll
  60. * @return mixed
  61. **/
  62. public function ChineseToPinyin($string, $isSimple = true, $isInitial = false,
  63. $isPolyphone = false, $isAll = false) {
  64. $result = '';
  65. if (empty($string)) {
  66. return $result;
  67. }
  68. $arrStringList = self::splitString($string);
  69. if (!is_array($arrStringList)) {
  70. return $result;
  71. }
  72. $arrPinyinList = self::toPinyinList($arrStringList);
  73. if (!is_array($arrPinyinList)) {
  74. return $result;
  75. }
  76. if ($isSimple === true) {
  77. foreach ($arrPinyinList as $arrPinyin) {
  78. if (empty($arrPinyin)) {
  79. continue;
  80. }
  81. $result .= $arrPinyin[0];
  82. }
  83. return $result;
  84. }
  85. $arrFirstPinyin = array_shift($arrPinyinList);
  86. if (($isInitial !== true) || ($isAll === true)) {
  87. $arrPrevPinyin = $arrFirstPinyin;
  88. foreach ($arrPinyinList as $arrPinyin) {
  89. $arrFullPinyin = array();
  90. foreach ($arrPrevPinyin as $strPrevPinyin) {
  91. foreach ($arrPinyin as $strPinyin) {
  92. $arrFullPinyin[] = $strPrevPinyin . $strPinyin;
  93. }
  94. }
  95. $arrPrevPinyin = $arrFullPinyin;
  96. }
  97. }
  98. if (($isInitial === true) || ($isAll === true)) {
  99. if (ord($arrFirstPinyin[0]) > 129) {
  100. $arrPrevInitialPinyin[0] = $arrFirstPinyin[0];
  101. } else {
  102. $arrPrevInitialPinyin[0] = substr($arrFirstPinyin[0], 0, 1);
  103. }
  104. foreach ($arrPinyinList as $arrPinyin) {
  105. $arrInitialPinyin = array();
  106. foreach ($arrPrevInitialPinyin as $strPrevPinyin) {
  107. foreach ($arrPinyin as $strPinyin) {
  108. if (ord($strPinyin) > 129) {
  109. $arrInitialPinyin[] = $strPrevPinyin . $strPinyin;
  110. } else {
  111. $arrInitialPinyin[] = $strPrevPinyin . substr($strPinyin, 0, 1);
  112. }
  113. }
  114. }
  115. $arrPrevInitialPinyin = $arrInitialPinyin;
  116. }
  117. }
  118. if ($isAll === true) {
  119. $result['full'] = $arrFullPinyin;
  120. $result['initial'] = $arrInitialPinyin;
  121. } elseif ($isPolyphone === true) {
  122. if (($isInitial === true)) {
  123. $result = $arrInitialPinyin;
  124. } else {
  125. $result = $arrFullPinyin;
  126. }
  127. } else {
  128. if (($isInitial === true)) {
  129. $result = reset($arrInitialPinyin);
  130. } else {
  131. $result = reset($arrFullPinyin);
  132. }
  133. }
  134. return $result;
  135. }
  136. /*
  137. *获取字符串首个字符/首个汉字拼音大写
  138. */
  139. public function get_first_char($string){
  140. $first = '';
  141. $pinyin = self::ChineseToPinyin($string);
  142. if(!empty($pinyin)){
  143. $first = strtoupper(substr($pinyin, 0, 1));
  144. }
  145. if (!empty($first) && (ord($first) < 65 || ord($first) > 90)) {
  146. return '';
  147. }
  148. return $first;
  149. }
  150. }