Board logo

标题: [转帖]简体中文转换为繁体中文的PHP函数 [打印本页]

作者: cnangel    时间: 2004-6-26 16:50     标题: [转帖]简体中文转换为繁体中文的PHP函数

感谢网友Keyes提供移植用的Delphi源代码。其调用方式为$txt=gbtobig5($txt)。
(注:源代码中的include "data_gb.php";这个文件在就是一个数组,http://caocao.oso.com.cn/data_gb.zip,请编辑下载到oso上,做一个链接,因为这个文件我过几天就要删除了。)
  1. <?
  2. /***********************************************************************
  3. Written by caocao
  4. caocao@eastday.com
  5. http://caocao.oso.com.cn ;
  6. With the help of Keyes
  7. Keyes2000@263.net
  8. http://my-wjl.scu.edu.cn/~Keyes ;
  9. ***********************************************************************/
  10. function isgb($code)
  11. {
  12. if (strlen($code)>=2)
  13. {
  14. $code=strtok($code,"");
  15. if ((ord($code[0]) < 161)||(ord($code[0]) >= 247))
  16. {
  17. return (0);
  18. }
  19. else
  20. {
  21. if ((ord($code[1]) <= 161)||(ord($code[1]) >= 254))
  22. {
  23. return (0);
  24. }
  25. else
  26. {
  27. return (1);
  28. }
  29. }
  30. }
  31. else
  32. {
  33. return (1);
  34. }
  35. }
  36. function gboffset($code)
  37. {
  38. if (strlen($code) >= 2)
  39. {
  40. $code=strtok($code,"");
  41. return ((ord($code[0]) - 161) * 94 + (ord($code[1]) - 161));
  42. }
  43. else
  44. {
  45. return(-1);
  46. }
  47. }
  48. function wordtostring($code)
  49. {
  50. return (chr(hexdec(substr($code,0,2))).chr(hexdec(substr($code,2,2))));
  51. }
  52. function gbtobig5($code)
  53. {
  54. include "data_gb.php";
  55. $output="";
  56. $length=strlen($code);
  57. $code=strtok($code,"");
  58. $idx=0;
  59. while ($idx < $length)
  60. {
  61. $tmpStr=$code[$idx].$code[$idx+1];
  62. if (isgb($tmpStr))
  63. {
  64. $offset=gboffset($tmpStr);
  65. if (($offset >= 0)||($offset <= 8177))
  66. {
  67. $output.=wordtostring($gborder[$offset]);
  68. $idx++;
  69. }
  70. else
  71. {
  72. $output.= $code[$idx];
  73. }
  74. }
  75. else
  76. {
  77. $output.= $code[$idx];
  78. }
  79. $idx++;
  80. }
  81. return ($output);
  82. };
  83. ?>
复制代码





欢迎光临 星星博客 (http://bbs.huhoo.net/) Powered by Discuz! 7.0.0