Convert.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace Pinyin\Lib;
  3. use Dever;
  4. Dever::apply('const', 'pinyin', 'overtrue/src');
  5. Dever::apply('DictLoaderInterface', 'pinyin', 'overtrue/src');
  6. Dever::apply('FileDictLoader', 'pinyin', 'overtrue/src');
  7. Dever::apply('GeneratorFileDictLoader', 'pinyin', 'overtrue/src');
  8. Dever::apply('MemoryFileDictLoader', 'pinyin', 'overtrue/src');
  9. Dever::apply('Pinyin', 'pinyin', 'overtrue/src');
  10. use Overtrue\Pinyin\Pinyin as Core;
  11. //教程http://overtrue.me/pinyin/
  12. class Convert
  13. {
  14. /**
  15. * @var null|Pinyin
  16. */
  17. protected $pinyin = null;
  18. public function __construct()
  19. {
  20. if (!$this->pinyin) {
  21. $this->pinyin = new Core('Overtrue\Pinyin\MemoryFileDictLoader');
  22. }
  23. }
  24. /**
  25. * 获取拼音类
  26. */
  27. public function getPinyinFunc()
  28. {
  29. return $this->pinyin;
  30. }
  31. /**
  32. * 获取拼音
  33. */
  34. public function getPinyin($string, $link = '')
  35. {
  36. return $this->pinyin->permalink($string, $link);
  37. }
  38. /**
  39. * 获取姓名
  40. */
  41. public function getPinyinName($string)
  42. {
  43. return $this->pinyin->name($string);
  44. }
  45. /**
  46. * 获取整段文字
  47. */
  48. public function getPinyinContent($string, $state = false)
  49. {
  50. return $this->pinyin->sentence($string, $state);
  51. }
  52. /**
  53. * 二维数组根据首字母分组排序
  54. * @param array $data 二维数组
  55. * @param string $targetKey 首字母的键名
  56. * @return array 根据首字母关联的二维数组
  57. */
  58. public function groupByPinyinFirst(array $data, $targetKey = 'name')
  59. {
  60. if (!$this->pinyin) {
  61. $this->pinyin = new Core('Overtrue\Pinyin\MemoryFileDictLoader');
  62. }
  63. $data = array_map(function ($item) use ($targetKey) {
  64. return array_merge($item, [
  65. 'first' => $this->getPinyinFirst($item[$targetKey]),
  66. ]);
  67. }, $data);
  68. $data = $this->sortPinyinFirst($data);
  69. return $data;
  70. }
  71. /**
  72. * 按字母排序
  73. * @param array $data
  74. * @return array
  75. */
  76. public function sortPinyinFirst(array $data, $first = 'first')
  77. {
  78. $sortData = [];
  79. foreach ($data as $key => $value) {
  80. $sortData[$value[$first]][] = $value;
  81. }
  82. ksort($sortData);
  83. if (isset($sortData[''])) {
  84. $tmp = $sortData[''];
  85. unset($sortData['']);
  86. $sortData[''] = $tmp;
  87. }
  88. return $sortData;
  89. }
  90. /**
  91. * 获取首字母
  92. * @param string $str 汉字字符串
  93. * @return string 首字母
  94. */
  95. public function getPinyinFirst($str)
  96. {
  97. if (empty($str)) {
  98. return '';
  99. }
  100. if (!$this->pinyin) {
  101. $this->pinyin = new Core('Overtrue\Pinyin\MemoryFileDictLoader');
  102. }
  103. return strtoupper(substr($this->pinyin->abbr($str, PINYIN_KEEP_ENGLISH), 0, 1));
  104. }
  105. }