暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChineseSpell.php 12KB

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