/phpsso_server/phpcms/libs/functions/iconv.func.php

https://github.com/hxzyzz/ddc · PHP · 251 lines · 242 code · 9 blank · 0 comment · 24 complexity · b0123c3e1102c0b69be3c88045efd1c9 MD5 · raw file

  1. <?php
  2. define('CODETABLEDIR', dirname(__FILE__).DIRECTORY_SEPARATOR.'encoding'.DIRECTORY_SEPARATOR);
  3. function utf8_to_gbk($utfstr)
  4. {
  5. global $UC2GBTABLE;
  6. $okstr = '';
  7. if(empty($UC2GBTABLE))
  8. {
  9. $filename = CODETABLEDIR.'gb-unicode.table';
  10. $fp = fopen($filename, 'rb');
  11. while($l = fgets($fp,15))
  12. {
  13. $UC2GBTABLE[hexdec(substr($l, 7, 6))] = hexdec(substr($l, 0, 6));
  14. }
  15. fclose($fp);
  16. }
  17. $okstr = '';
  18. $ulen = strlen($utfstr);
  19. for($i=0; $i<$ulen; $i++)
  20. {
  21. $c = $utfstr[$i];
  22. $cb = decbin(ord($utfstr[$i]));
  23. if(strlen($cb)==8)
  24. {
  25. $csize = strpos(decbin(ord($cb)),'0');
  26. for($j = 0; $j < $csize; $j++)
  27. {
  28. $i++;
  29. $c .= $utfstr[$i];
  30. }
  31. $c = utf8_to_unicode($c);
  32. if(isset($UC2GBTABLE[$c]))
  33. {
  34. $c = dechex($UC2GBTABLE[$c]+0x8080);
  35. $okstr .= chr(hexdec($c[0].$c[1])).chr(hexdec($c[2].$c[3]));
  36. }
  37. else
  38. {
  39. $okstr .= '&#'.$c.';';
  40. }
  41. }
  42. else
  43. {
  44. $okstr .= $c;
  45. }
  46. }
  47. $okstr = trim($okstr);
  48. return $okstr;
  49. }
  50. function gbk_to_utf8($gbstr)
  51. {
  52. global $CODETABLE;
  53. if(empty($CODETABLE))
  54. {
  55. $filename = CODETABLEDIR.'gb-unicode.table';
  56. $fp = fopen($filename, 'rb');
  57. while($l = fgets($fp,15))
  58. {
  59. $CODETABLE[hexdec(substr($l, 0, 6))] = substr($l, 7, 6);
  60. }
  61. fclose($fp);
  62. }
  63. $ret = '';
  64. $utf8 = '';
  65. while($gbstr)
  66. {
  67. if(ord(substr($gbstr, 0, 1)) > 0x80)
  68. {
  69. $thisW = substr($gbstr, 0, 2);
  70. $gbstr = substr($gbstr, 2, strlen($gbstr));
  71. $utf8 = '';
  72. @$utf8 = unicode_to_utf8(hexdec($CODETABLE[hexdec(bin2hex($thisW)) - 0x8080]));
  73. if($utf8 != '')
  74. {
  75. for($i = 0; $i < strlen($utf8); $i += 3) $ret .= chr(substr($utf8, $i, 3));
  76. }
  77. }
  78. else
  79. {
  80. $ret .= substr($gbstr, 0, 1);
  81. $gbstr = substr($gbstr, 1, strlen($gbstr));
  82. }
  83. }
  84. return $ret;
  85. }
  86. function big5_to_gbk($Text)
  87. {
  88. global $BIG5_DATA;
  89. if(empty($BIG5_DATA))
  90. {
  91. $filename = CODETABLEDIR.'big5-gb.table';
  92. $fp = fopen($filename, 'rb');
  93. $BIG5_DATA = fread($fp, filesize($filename));
  94. fclose($fp);
  95. }
  96. $max = strlen($Text)-1;
  97. for($i = 0; $i < $max; $i++)
  98. {
  99. $h = ord($Text[$i]);
  100. if($h >= 0x80)
  101. {
  102. $l = ord($Text[$i+1]);
  103. if($h==161 && $l==64)
  104. {
  105. $gbstr = '¡¡';
  106. }
  107. else
  108. {
  109. $p = ($h-160)*510+($l-1)*2;
  110. $gbstr = $BIG5_DATA[$p].$BIG5_DATA[$p+1];
  111. }
  112. $Text[$i] = $gbstr[0];
  113. $Text[$i+1] = $gbstr[1];
  114. $i++;
  115. }
  116. }
  117. return $Text;
  118. }
  119. function gbk_to_big5($Text)
  120. {
  121. global $GB_DATA;
  122. if(empty($GB_DATA))
  123. {
  124. $filename = CODETABLEDIR.'gb-big5.table';
  125. $fp = fopen($filename, 'rb');
  126. $gb = fread($fp, filesize($filename));
  127. fclose($fp);
  128. }
  129. $max = strlen($Text)-1;
  130. for($i = 0; $i < $max; $i++)
  131. {
  132. $h = ord($Text[$i]);
  133. if($h >= 0x80)
  134. {
  135. $l = ord($Text[$i+1]);
  136. if($h==161 && $l==64)
  137. {
  138. $big = '¡¡';
  139. }
  140. else
  141. {
  142. $p = ($h-160)*510+($l-1)*2;
  143. $big = $GB_DATA[$p].$GB_DATA[$p+1];
  144. }
  145. $Text[$i] = $big[0];
  146. $Text[$i+1] = $big[1];
  147. $i++;
  148. }
  149. }
  150. return $Text;
  151. }
  152. function unicode_to_utf8($c)
  153. {
  154. $str = '';
  155. if($c < 0x80)
  156. {
  157. $str .= $c;
  158. }
  159. elseif($c < 0x800)
  160. {
  161. $str .= (0xC0 | $c >> 6);
  162. $str .= (0x80 | $c & 0x3F);
  163. }
  164. elseif($c < 0x10000)
  165. {
  166. $str .= (0xE0 | $c >> 12);
  167. $str .= (0x80 | $c >> 6 & 0x3F);
  168. $str .= (0x80 | $c & 0x3F);
  169. }
  170. elseif($c < 0x200000)
  171. {
  172. $str .= (0xF0 | $c >> 18);
  173. $str .= (0x80 | $c >> 12 & 0x3F);
  174. $str .= (0x80 | $c >> 6 & 0x3F);
  175. $str .= (0x80 | $c & 0x3F);
  176. }
  177. return $str;
  178. }
  179. function utf8_to_unicode($c)
  180. {
  181. switch(strlen($c))
  182. {
  183. case 1:
  184. return ord($c);
  185. case 2:
  186. $n = (ord($c[0]) & 0x3f) << 6;
  187. $n += ord($c[1]) & 0x3f;
  188. return $n;
  189. case 3:
  190. $n = (ord($c[0]) & 0x1f) << 12;
  191. $n += (ord($c[1]) & 0x3f) << 6;
  192. $n += ord($c[2]) & 0x3f;
  193. return $n;
  194. case 4:
  195. $n = (ord($c[0]) & 0x0f) << 18;
  196. $n += (ord($c[1]) & 0x3f) << 12;
  197. $n += (ord($c[2]) & 0x3f) << 6;
  198. $n += ord($c[3]) & 0x3f;
  199. return $n;
  200. }
  201. }
  202. function asc_to_pinyin($asc,&$pyarr)
  203. {
  204. if($asc < 128)return chr($asc);
  205. elseif(isset($pyarr[$asc]))return $pyarr[$asc];
  206. else
  207. {
  208. foreach($pyarr as $id => $p)
  209. {
  210. if($id >= $asc)return $p;
  211. }
  212. }
  213. }
  214. function gbk_to_pinyin($txt)
  215. {
  216. $l = strlen($txt);
  217. $i = 0;
  218. $pyarr = array();
  219. $py = array();
  220. $filename = CODETABLEDIR.'gb-pinyin.table';
  221. $fp = fopen($filename,'r');
  222. while(!feof($fp))
  223. {
  224. $p = explode("-",fgets($fp,32));
  225. $pyarr[intval($p[1])] = trim($p[0]);
  226. }
  227. fclose($fp);
  228. ksort($pyarr);
  229. while($i<$l)
  230. {
  231. $tmp = ord($txt[$i]);
  232. if($tmp>=128)
  233. {
  234. $asc = abs($tmp*256+ord($txt[$i+1])-65536);
  235. $i = $i+1;
  236. }else $asc = $tmp;
  237. $py[] = asc_to_pinyin($asc,$pyarr);
  238. $i++;
  239. }
  240. return $py;
  241. }
  242. ?>