返回列表 回复 发帖
所需阅读权限 1

[转帖]简体中文转换为繁体中文的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. ?>
复制代码

                     我是一个呼吸着现在的空气而生活在过去的人
               这样的注定孤独,孤独的身处闹市却犹如置身于荒漠
                                     我已习惯了孤独,爱上孤独
                                 他让我看清了自我,还原了自我
                             让我再静静的沉思中得到快乐和满足
                                   再孤独的世界里我一遍又一遍
                                   不厌其烦的改写着自己的过去
                                             延伸到现在与未来
                                       然而那只是泡沫般的美梦
                                 产生的时刻又伴随着破灭的到来
                         在灰飞烟灭的瞬间我看到的是过程的美丽
                                      而不是结果的悲哀。。。
返回列表