Pinyin.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Qii\Library;
  3. use Yurun\Util\Chinese;
  4. \Qii\Autoloader\Psr4::getInstance()
  5. ->setUseNamespaces([
  6. ['Yurun\Util', true],
  7. ])
  8. ->addNamespaces([
  9. ['Yurun\Util', Qii_DIR . DS .'Library'. DS . 'Third'. DS .'ChineseUtil'. DS . 'src'. DS],
  10. ]);
  11. /**
  12. * 拼音
  13. *
  14. */
  15. class Pinyin
  16. {
  17. public function __construct()
  18. {
  19. }
  20. /**
  21. * 获取每个字的全拼
  22. * @param string $str 文字
  23. */
  24. public function full($str)
  25. {
  26. return Chinese::toPinyin($str, Chinese\Pinyin::CONVERT_MODE_PINYIN)['pinyin'];
  27. }
  28. /**
  29. * 返回拼音、首字母、读音等
  30. * @param string $str
  31. * @return array
  32. */
  33. public function toPinyin($str) {
  34. return Chinese::toPinyin($str);
  35. }
  36. /**
  37. * 获取每个字的拼音首字母
  38. *
  39. * @param string $str 文字
  40. * @return mixed
  41. */
  42. public function begins($str)
  43. {
  44. return Chinese::toPinyin($str, Chinese\Pinyin::CONVERT_MODE_PINYIN_FIRST)['pinyinFirst'];
  45. }
  46. /**
  47. * 获取每个字的读音
  48. *
  49. * @param string $str 需要转换的内容
  50. * @return mixed
  51. */
  52. public function sounds($str)
  53. {
  54. return Chinese::toPinyin($str, Chinese\Pinyin::CONVERT_MODE_PINYIN_SOUND)['pinyinSound'];
  55. }
  56. /**
  57. * 获取每个字的读音数字
  58. * @param string $str 需要转换的内容
  59. * @return mixed
  60. */
  61. public function soundsNumber($str)
  62. {
  63. return Chinese::toPinyin($str, Chinese\Pinyin::CONVERT_MODE_PINYIN_SOUND_NUMBER)['pinyinSoundNumber'];
  64. }
  65. /**
  66. * 转换成简体
  67. *
  68. * @param string $str 需要转换的内容
  69. * @return mixed
  70. */
  71. public function toSimplified($str)
  72. {
  73. return Chinese::toSimplified($str);
  74. }
  75. /**
  76. * 转换成繁体
  77. *
  78. * @param string $str 需要转换的内容
  79. * @return mixed
  80. */
  81. public function toTraditional($str)
  82. {
  83. return Chinese::toTraditional($str);
  84. }
  85. }