/phpcms程序模板开发/phpcms/phpcms/libs/functions/iconv.func.php

https://github.com/houdunwang/video · PHP · 265 lines · 223 code · 1 blank · 41 comment · 41 complexity · 4616313173c44bb4988549f7854fe91b MD5 · raw file

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