ChinaSpell.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\ChinaArea;
  4. use think\Db;
  5. /**
  6. * @title 汉字拼音
  7. * @controller Area
  8. * @group common
  9. */
  10. class ChinaSpell extends Base
  11. {
  12. /**
  13. * @var array $chineseSpellList 拼音编码对应表
  14. * @access private
  15. */
  16. private $chineseSpellList = array(
  17. 'a'=>-20319,
  18. 'ai'=>-20317,
  19. 'an'=>-20304,
  20. 'ang'=>-20295,
  21. 'ao'=>-20292,
  22. 'ba'=>-20283,
  23. 'bai'=>-20265,
  24. 'ban'=>-20257,
  25. 'bang'=>-20242,
  26. 'bao'=>-20230,
  27. 'bei'=>-20051,
  28. 'ben'=>-20036,
  29. 'beng'=>-20032,
  30. 'bi'=>-20026,
  31. 'bian'=>-20002,
  32. 'biao'=>-19990,
  33. 'bie'=>-19986,
  34. 'bin'=>-19982,
  35. 'bing'=>-19976,
  36. 'bo'=>-19805,
  37. 'bu'=>-19784,
  38. 'ca'=>-19775,
  39. 'cai'=>-19774,
  40. 'can'=>-19763,
  41. 'cang'=>-19756,
  42. 'cao'=>-19751,
  43. 'ce'=>-19746,
  44. 'ceng'=>-19741,
  45. 'cha'=>-19739,
  46. 'chai'=>-19728,
  47. 'chan'=>-19725,
  48. 'chang'=>-19715,
  49. 'chao'=>-19540,
  50. 'che'=>-19531,
  51. 'chen'=>-19525,
  52. 'cheng'=>-19515,
  53. 'chi'=>-19500,
  54. 'chong'=>-19484,
  55. 'chou'=>-19479,
  56. 'chu'=>-19467,
  57. 'chuai'=>-19289,
  58. 'chuan'=>-19288,
  59. 'chuang'=>-19281,
  60. 'chui'=>-19275,
  61. 'chun'=>-19270,
  62. 'chuo'=>-19263,
  63. 'ci'=>-19261,
  64. 'cong'=>-19249,
  65. 'cou'=>-19243,
  66. 'cu'=>-19242,
  67. 'cuan'=>-19238,
  68. 'cui'=>-19235,
  69. 'cun'=>-19227,
  70. 'cuo'=>-19224,
  71. 'da'=>-19218,
  72. 'dai'=>-19212,
  73. 'dan'=>-19038,
  74. 'dang'=>-19023,
  75. 'dao'=>-19018,
  76. 'de'=>-19006,
  77. 'deng'=>-19003,
  78. 'di'=>-18996,
  79. 'dian'=>-18977,
  80. 'diao'=>-18961,
  81. 'die'=>-18952,
  82. 'ding'=>-18783,
  83. 'diu'=>-18774,
  84. 'dong'=>-18773,
  85. 'dou'=>-18763,
  86. 'du'=>-18756,
  87. 'duan'=>-18741,
  88. 'dui'=>-18735,
  89. 'dun'=>-18731,
  90. 'duo'=>-18722,
  91. 'e'=>-18710,
  92. 'en'=>-18697,
  93. 'er'=>-18696,
  94. 'fa'=>-18526,
  95. 'fan'=>-18518,
  96. 'fang'=>-18501,
  97. 'fei'=>-18490,
  98. 'fen'=>-18478,
  99. 'feng'=>-18463,
  100. 'fo'=>-18448,
  101. 'fou'=>-18447,
  102. 'fu'=>-18446,
  103. 'ga'=>-18239,
  104. 'gai'=>-18237,
  105. 'gan'=>-18231,
  106. 'gang'=>-18220,
  107. 'gao'=>-18211,
  108. 'ge'=>-18201,
  109. 'gei'=>-18184,
  110. 'gen'=>-18183,
  111. 'geng'=>-18181,
  112. 'gong'=>-18012,
  113. 'gou'=>-17997,
  114. 'gu'=>-17988,
  115. 'gua'=>-17970,
  116. 'guai'=>-17964,
  117. 'guan'=>-17961,
  118. 'guang'=>-17950,
  119. 'gui'=>-17947,
  120. 'gun'=>-17931,
  121. 'guo'=>-17928,
  122. 'ha'=>-17922,
  123. 'hai'=>-17759,
  124. 'han'=>-17752,
  125. 'hang'=>-17733,
  126. 'hao'=>-17730,
  127. 'he'=>-17721,
  128. 'hei'=>-17703,
  129. 'hen'=>-17701,
  130. 'heng'=>-17697,
  131. 'hong'=>-17692,
  132. 'hou'=>-17683,
  133. 'hu'=>-17676,
  134. 'hua'=>-17496,
  135. 'huai'=>-17487,
  136. 'huan'=>-17482,
  137. 'huang'=>-17468,
  138. 'hui'=>-17454,
  139. 'hun'=>-17433,
  140. 'huo'=>-17427,
  141. 'ji'=>-17417,
  142. 'jia'=>-17202,
  143. 'jian'=>-17185,
  144. 'jiang'=>-16983,
  145. 'jiao'=>-16970,
  146. 'jie'=>-16942,
  147. 'jin'=>-16915,
  148. 'jing'=>-16733,
  149. 'jiong'=>-16708,
  150. 'jiu'=>-16706,
  151. 'ju'=>-16689,
  152. 'juan'=>-16664,
  153. 'jue'=>-16657,
  154. 'jun'=>-16647,
  155. 'ka'=>-16474,
  156. 'kai'=>-16470,
  157. 'kan'=>-16465,
  158. 'kang'=>-16459,
  159. 'kao'=>-16452,
  160. 'ke'=>-16448,
  161. 'ken'=>-16433,
  162. 'keng'=>-16429,
  163. 'kong'=>-16427,
  164. 'kou'=>-16423,
  165. 'ku'=>-16419,
  166. 'kua'=>-16412,
  167. 'kuai'=>-16407,
  168. 'kuan'=>-16403,
  169. 'kuang'=>-16401,
  170. 'kui'=>-16393,
  171. 'kun'=>-16220,
  172. 'kuo'=>-16216,
  173. 'la'=>-16212,
  174. 'lai'=>-16205,
  175. 'lan'=>-16202,
  176. 'lang'=>-16187,
  177. 'lao'=>-16180,
  178. 'le'=>-16171,
  179. 'lei'=>-16169,
  180. 'leng'=>-16158,
  181. 'li'=>-16155,
  182. 'lia'=>-15959,
  183. 'lian'=>-15958,
  184. 'liang'=>-15944,
  185. 'liao'=>-15933,
  186. 'lie'=>-15920,
  187. 'lin'=>-15915,
  188. 'ling'=>-15903,
  189. 'liu'=>-15889,
  190. 'long'=>-15878,
  191. 'lou'=>-15707,
  192. 'lu'=>-15701,
  193. 'lv'=>-15681,
  194. 'luan'=>-15667,
  195. 'lue'=>-15661,
  196. 'lun'=>-15659,
  197. 'luo'=>-15652,
  198. 'ma'=>-15640,
  199. 'mai'=>-15631,
  200. 'man'=>-15625,
  201. 'mang'=>-15454,
  202. 'mao'=>-15448,
  203. 'me'=>-15436,
  204. 'mei'=>-15435,
  205. 'men'=>-15419,
  206. 'meng'=>-15416,
  207. 'mi'=>-15408,
  208. 'mian'=>-15394,
  209. 'miao'=>-15385,
  210. 'mie'=>-15377,
  211. 'min'=>-15375,
  212. 'ming'=>-15369,
  213. 'miu'=>-15363,
  214. 'mo'=>-15362,
  215. 'mou'=>-15183,
  216. 'mu'=>-15180,
  217. 'na'=>-15165,
  218. 'nai'=>-15158,
  219. 'nan'=>-15153,
  220. 'nang'=>-15150,
  221. 'nao'=>-15149,
  222. 'ne'=>-15144,
  223. 'nei'=>-15143,
  224. 'nen'=>-15141,
  225. 'neng'=>-15140,
  226. 'ni'=>-15139,
  227. 'nian'=>-15128,
  228. 'niang'=>-15121,
  229. 'niao'=>-15119,
  230. 'nie'=>-15117,
  231. 'nin'=>-15110,
  232. 'ning'=>-15109,
  233. 'niu'=>-14941,
  234. 'nong'=>-14937,
  235. 'nu'=>-14933,
  236. 'nv'=>-14930,
  237. 'nuan'=>-14929,
  238. 'nue'=>-14928,
  239. 'nuo'=>-14926,
  240. 'o'=>-14922,
  241. 'ou'=>-14921,
  242. 'pa'=>-14914,
  243. 'pai'=>-14908,
  244. 'pan'=>-14902,
  245. 'pang'=>-14894,
  246. 'pao'=>-14889,
  247. 'pei'=>-14882,
  248. 'pen'=>-14873,
  249. 'peng'=>-14871,
  250. 'pi'=>-14857,
  251. 'pian'=>-14678,
  252. 'piao'=>-14674,
  253. 'pie'=>-14670,
  254. 'pin'=>-14668,
  255. 'ping'=>-14663,
  256. 'po'=>-14654,
  257. 'pu'=>-14645,
  258. 'qi'=>-14630,
  259. 'qia'=>-14594,
  260. 'qian'=>-14429,
  261. 'qiang'=>-14407,
  262. 'qiao'=>-14399,
  263. 'qie'=>-14384,
  264. 'qin'=>-14379,
  265. 'qing'=>-14368,
  266. 'qiong'=>-14355,
  267. 'qiu'=>-14353,
  268. 'qu'=>-14345,
  269. 'quan'=>-14170,
  270. 'que'=>-14159,
  271. 'qun'=>-14151,
  272. 'ran'=>-14149,
  273. 'rang'=>-14145,
  274. 'rao'=>-14140,
  275. 're'=>-14137,
  276. 'ren'=>-14135,
  277. 'reng'=>-14125,
  278. 'ri'=>-14123,
  279. 'rong'=>-14122,
  280. 'rou'=>-14112,
  281. 'ru'=>-14109,
  282. 'ruan'=>-14099,
  283. 'rui'=>-14097,
  284. 'run'=>-14094,
  285. 'ruo'=>-14092,
  286. 'sa'=>-14090,
  287. 'sai'=>-14087,
  288. 'san'=>-14083,
  289. 'sang'=>-13917,
  290. 'sao'=>-13914,
  291. 'se'=>-13910,
  292. 'sen'=>-13907,
  293. 'seng'=>-13906,
  294. 'sha'=>-13905,
  295. 'shai'=>-13896,
  296. 'shan'=>-13894,
  297. 'shang'=>-13878,
  298. 'shao'=>-13870,
  299. 'she'=>-13859,
  300. 'shen'=>-13847,
  301. 'sheng'=>-13831,
  302. 'shi'=>-13658,
  303. 'shou'=>-13611,
  304. 'shu'=>-13601,
  305. 'shua'=>-13406,
  306. 'shuai'=>-13404,
  307. 'shuan'=>-13400,
  308. 'shuang'=>-13398,
  309. 'shui'=>-13395,
  310. 'shun'=>-13391,
  311. 'shuo'=>-13387,
  312. 'si'=>-13383,
  313. 'song'=>-13367,
  314. 'sou'=>-13359,
  315. 'su'=>-13356,
  316. 'suan'=>-13343,
  317. 'sui'=>-13340,
  318. 'sun'=>-13329,
  319. 'suo'=>-13326,
  320. 'ta'=>-13318,
  321. 'tai'=>-13147,
  322. 'tan'=>-13138,
  323. 'tang'=>-13120,
  324. 'tao'=>-13107,
  325. 'te'=>-13096,
  326. 'teng'=>-13095,
  327. 'ti'=>-13091,
  328. 'tian'=>-13076,
  329. 'tiao'=>-13068,
  330. 'tie'=>-13063,
  331. 'ting'=>-13060,
  332. 'tong'=>-12888,
  333. 'tou'=>-12875,
  334. 'tu'=>-12871,
  335. 'tuan'=>-12860,
  336. 'tui'=>-12858,
  337. 'tun'=>-12852,
  338. 'tuo'=>-12849,
  339. 'wa'=>-12838,
  340. 'wai'=>-12831,
  341. 'wan'=>-12829,
  342. 'wang'=>-12812,
  343. 'wei'=>-12802,
  344. 'wen'=>-12607,
  345. 'weng'=>-12597,
  346. 'wo'=>-12594,
  347. 'wu'=>-12585,
  348. 'xi'=>-12556,
  349. 'xia'=>-12359,
  350. 'xian'=>-12346,
  351. 'xiang'=>-12320,
  352. 'xiao'=>-12300,
  353. 'xie'=>-12120,
  354. 'xin'=>-12099,
  355. 'xing'=>-12089,
  356. 'xiong'=>-12074,
  357. 'xiu'=>-12067,
  358. 'xu'=>-12058,
  359. 'xuan'=>-12039,
  360. 'xue'=>-11867,
  361. 'xun'=>-11861,
  362. 'ya'=>-11847,
  363. 'yan'=>-11831,
  364. 'yang'=>-11798,
  365. 'yao'=>-11781,
  366. 'ye'=>-11604,
  367. 'yi'=>-11589,
  368. 'yin'=>-11536,
  369. 'ying'=>-11358,
  370. 'yo'=>-11340,
  371. 'yong'=>-11339,
  372. 'you'=>-11324,
  373. 'yu'=>-11303,
  374. 'yuan'=>-11097,
  375. 'yue'=>-11077,
  376. 'yun'=>-11067,
  377. 'za'=>-11055,
  378. 'zai'=>-11052,
  379. 'zan'=>-11045,
  380. 'zang'=>-11041,
  381. 'zao'=>-11038,
  382. 'ze'=>-11024,
  383. 'zei'=>-11020,
  384. 'zen'=>-11019,
  385. 'zeng'=>-11018,
  386. 'zha'=>-11014,
  387. 'zhai'=>-10838,
  388. 'zhan'=>-10832,
  389. 'zhang'=>-10815,
  390. 'zhao'=>-10800,
  391. 'zhe'=>-10790,
  392. 'zhen'=>-10780,
  393. 'zheng'=>-10764,
  394. 'zhi'=>-10587,
  395. 'zhong'=>-10544,
  396. 'zhou'=>-10533,
  397. 'zhu'=>-10519,
  398. 'zhua'=>-10331,
  399. 'zhuai'=>-10329,
  400. 'zhuan'=>-10328,
  401. 'zhuang'=>-10322,
  402. 'zhui'=>-10315,
  403. 'zhun'=>-10309,
  404. 'zhuo'=>-10307,
  405. 'zi'=>-10296,
  406. 'zong'=>-10281,
  407. 'zou'=>-10274,
  408. 'zu'=>-10270,
  409. 'zuan'=>-10262,
  410. 'zui'=>-10260,
  411. 'zun'=>-10256,
  412. 'zuo'=>-10254
  413. );
  414. //得到汉字拼音首字母
  415. public function getFirstChar($str){
  416. $fchar = ord($str{0});
  417. if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($str{0});
  418. $s1 = mb_convert_encoding($str,"gb2312");
  419. $s2 = mb_convert_encoding($s1, "UTF-8");
  420. if($s2 == $str){$s = $s1;}else{$s = $str;}
  421. $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
  422. if($asc >= -20319 and $asc <= -20284) return "A";
  423. if($asc >= -20283 and $asc <= -19776) return "B";
  424. if($asc >= -19775 and $asc <= -19219) return "C";
  425. if($asc >= -19218 and $asc <= -18711) return "D";
  426. if($asc >= -18710 and $asc <= -18527) return "E";
  427. if($asc >= -18526 and $asc <= -18240) return "F";
  428. if($asc >= -18239 and $asc <= -17923) return "G";
  429. if($asc >= -17922 and $asc <= -17418) return "H";
  430. if($asc >= -17922 and $asc <= -17418) return "I";
  431. if($asc >= -17417 and $asc <= -16475) return "J";
  432. if($asc >= -16474 and $asc <= -16213) return "K";
  433. if($asc >= -16212 and $asc <= -15641) return "L";
  434. if($asc >= -15640 and $asc <= -15166) return "M";
  435. if($asc >= -15165 and $asc <= -14923) return "N";
  436. if($asc >= -14922 and $asc <= -14915) return "O";
  437. if($asc >= -14914 and $asc <= -14631) return "P";
  438. if($asc >= -14630 and $asc <= -14150) return "Q";
  439. if($asc >= -14149 and $asc <= -14091) return "R";
  440. if($asc >= -14090 and $asc <= -13319) return "S";
  441. if($asc >= -13318 and $asc <= -12839) return "T";
  442. if($asc >= -12838 and $asc <= -12557) return "W";
  443. if($asc >= -12556 and $asc <= -11848) return "X";
  444. if($asc >= -11847 and $asc <= -11056) return "Y";
  445. if($asc >= -11055 and $asc <= -10247) return "Z";
  446. return NULL;
  447. }
  448. public function pinYinLong($zh)
  449. { //获取整条字符串汉字拼音首字母
  450. $ret = "";
  451. $s1 = iconv("UTF-8", "gb2312", $zh);
  452. $s2 = iconv("gb2312", "UTF-8", $s1);
  453. if ($s2 == $zh) {
  454. $zh = $s1;
  455. }
  456. for ($i = 0; $i < strlen($zh); $i++) {
  457. $s1 = substr($zh, $i, 1);
  458. $p = ord($s1);
  459. if ($p > 160) {
  460. $s2 = substr($zh, $i++, 2);
  461. $ret .= $this->getFirstChar($s2);
  462. } else {
  463. $ret .= $s1;
  464. }
  465. }
  466. return $ret;
  467. }
  468. /**
  469. * 取汉字所有拼音
  470. * @param string $chinese 要转换的汉字
  471. * @param string $delimiter 分隔符
  472. * @param int $length 返回的长度
  473. * @return string
  474. */
  475. public function getFullSpell($chinese, $delimiter = '', $length = 0){
  476. $spell = $this->getChineseSpells($chinese, $delimiter);
  477. if($length){
  478. $spell = substr($spell, 0, $length);
  479. }
  480. return $spell;
  481. }
  482. /**
  483. * 取一个汉字码对应的拼音
  484. * @param int $num 汉字码
  485. * @param string $blank 空白字符
  486. * @return string
  487. */
  488. public function getChineseSpell ($num, $blank = ''){
  489. if($num>0 && $num<160){
  490. return chr($num);
  491. }
  492. elseif($num<-20319||$num>-10247){
  493. return $blank;
  494. }
  495. else{
  496. foreach ($this->chineseSpellList as $spell => $code){
  497. if ($code > $num) break;
  498. $result = $spell;
  499. }
  500. return $result;
  501. }
  502. }
  503. /**
  504. * 取汉字拼音
  505. * @param string $chinese 要转换的汉字
  506. * @param string $delimiter 分隔符
  507. * @param int $first 是否只返回第一个
  508. * @return string
  509. */
  510. public function getChineseSpells($chinese, $delimiter = '', $first=0){
  511. $chinese = mb_convert_encoding($chinese, "GBK");
  512. $result = array();
  513. for($i=0; $i<strlen($chinese); $i++){
  514. $p = ord(substr($chinese,$i,1));
  515. if($p>160){
  516. $q = ord(substr($chinese,++$i,1));
  517. $p = $p*256 + $q - 65536;
  518. }
  519. $result[] = $this->getChineseSpell($p);
  520. if($first){
  521. return $result[0];
  522. }
  523. }
  524. return implode($delimiter, $result);
  525. }
  526. }