繁体中文转换为简体中文的PHP函数


Posted in PHP onOctober 09, 2006

前几天我在oso论坛上问有没有这个源程序,没人回复。终于下定决心移植一个这样的函数,感谢网友Keyes提供移植用的Delphi源代码。其调用方式为$txt=big5togb($txt)。
    (注:源代码中的include "data_big5.php";这个文件在就是一个数组,在http://caocao.oso.com.cn/data_big5.zip,请编辑下载到oso上,做一个链接,因为这个文件我过几天就要删除了。)

<?
/***********************************************************************
                       Written by caocao
                       caocao@eastday.com
                       http://caocao.oso.com.cn

                       With the help of Keyes
                       Keyes2000@263.net
                       http://my-wjl.scu.edu.cn/~Keyes
***********************************************************************/
function isbig5($code)
{
  if (strlen($code)>=2)
  {
    $code=strtok($code,"");

    if (ord($code[0]) < 161)
    {
      return (0);
    }
    else
    {
      if (((ord($code[1]) >= 64)&&(ord($code[1]) <= 126))||((ord($code[1]) >= 161)&&(ord($code[1]) <= 254)))
      {
        return (1);
      }
      else
      {
        return (0);
      }
    }
  }
  else
  {
    return (0);
  }
}

function big5offset($code)
{
  if (strlen($code) >= 2)
  {
    $code=strtok($code,"");
    if ((ord($code[1]) >= 64)&&(ord($code[1]) <= 126))
    {
      return ((ord($code[0]) - 161) * 157 + (ord($code[1]) - 64));
    }
    if ((ord($code[1]) >= 161)&&(ord($code[1]) <= 254))
    {
      return ((ord($code[0]) - 161) * 157 + 63 + (ord($code[1]) - 161));
    }
  }
  return (-1);
}

function wordtostring($code)
{
  return (chr(hexdec(substr($code,0,2))).chr(hexdec(substr($code,2,2))));
}

function big5togb($code)
{
  include "data_big5.php";
  $output="";
  $length=strlen($code);
  $code=strtok($code,"");
  $idx=0;
  while ($idx < $length)
  {
    $tmpStr=$code[$idx].$code[$idx+1];

    if (isbig5($tmpStr))
    {
      $offset=big5offset($tmpStr);
      if (($offset >= 0)||($offset <= 14757))
      {
        $output.=wordtostring($big5order[$offset]);
        $idx++;
      }
      else
      {
        $output.= $code[$idx];
      }
    }
    else
    {
      $output.= $code[$idx];
    }
    $idx++;
  }
  return ($output);
}
?>  

【本文版权归作者与奥索网共同拥有,如需转载,请注明作者及出处】    

PHP 相关文章推荐
BBS(php &amp; mysql)完整版(七)
Oct 09 PHP
聊天室php&amp;mysql(三)
Oct 09 PHP
php中通过Ajax如何实现异步文件上传的代码实例
May 07 PHP
linux环境apache多端口配置虚拟主机的方法深入介绍
Jun 09 PHP
PHP仿博客园 个人博客(2) 数据库增添改删
Jul 05 PHP
yii去掉必填项中星号的方法
Dec 28 PHP
php打包网站并在线压缩为zip
Feb 13 PHP
什么是OneThink oneThink后台添加插件步骤
Apr 13 PHP
基于jQueryUI和Corethink实现百度的搜索提示功能
Nov 09 PHP
php验证码生成器
May 24 PHP
PHP自定义递归函数实现数组转JSON功能【支持GBK编码】
Jul 17 PHP
laravel框架之数据库查出来的对象实现转化为数组
Oct 23 PHP
简体中文转换为繁体中文的PHP函数
Oct 09 #PHP
模仿OSO的论坛(四)
Oct 09 #PHP
二十行语句实现从Excel到mysql的转化
Oct 09 #PHP
一个颜色轮换的简单例子
Oct 09 #PHP
让你的PHP同时支持GIF、png、JPEG
Oct 09 #PHP
定制404错误页面,并发信给管理员的程序
Oct 09 #PHP
php录入页面中动态从数据库中提取数据的实现
Oct 09 #PHP
You might like
德生H-501的评价与改造
2021/03/02 无线电
php操作memcache缓存方法分享
2015/06/03 PHP
百万级别知乎用户数据抓取与分析之PHP开发
2015/09/28 PHP
Zend Framework实现Zend_View集成Smarty模板系统的方法
2016/03/05 PHP
PHP文件系统管理(实例讲解)
2017/09/19 PHP
js 禁用只读文本框获得焦点时的退格键
2010/04/25 Javascript
js/jquery解析json和数组格式的方法详解
2014/01/09 Javascript
js实现的黑背景灰色二级导航菜单效果代码
2015/08/24 Javascript
基于Bootstrap实现下拉菜单项和表单导航条(两个菜单项,一个下拉菜单和登录表单导航条)
2016/07/22 Javascript
常用原生js自定义函数总结
2016/11/20 Javascript
基于js实现二级下拉联动
2016/12/17 Javascript
微信小程序 request接口的封装实例代码
2017/04/26 Javascript
Angular动态绑定样式及改变UI框架样式的方法小结
2018/09/03 Javascript
Vue中 v-if 和v-else-if页面加载出现闪现的问题及解决方法
2018/10/12 Javascript
jquery实现直播弹幕效果
2019/11/28 jQuery
vue学习笔记之slot插槽基本用法实例分析
2020/02/01 Javascript
[02:28]DOTA2亚洲邀请赛附加赛 RECAP赛事回顾
2015/01/29 DOTA
python 装饰器功能以及函数参数使用介绍
2012/01/27 Python
在Python的struct模块中进行数据格式转换的方法
2015/06/17 Python
python+opencv轮廓检测代码解析
2018/01/05 Python
numpy的文件存储.npy .npz 文件详解
2018/07/09 Python
python hook监听事件详解
2018/10/25 Python
python2和python3实现在图片上加汉字的方法
2019/08/22 Python
pycharm修改file type方式
2019/11/19 Python
基于Python生成个性二维码过程详解
2020/03/05 Python
PyQt5 界面显示无响应的实现
2020/03/26 Python
Softmax函数原理及Python实现过程解析
2020/05/22 Python
经验丰富程序员才知道的8种高级Python技巧
2020/07/27 Python
python 实现性别识别
2020/11/21 Python
西班牙创意礼品和小工具网上商店:Curiosite
2016/07/26 全球购物
JAVA和C++区别都有哪些
2015/03/30 面试题
学校党委干部个人对照检查材料思想汇报
2014/10/09 职场文书
公司租房协议书
2014/10/14 职场文书
2015年董事长秘书工作总结
2015/07/23 职场文书
用python实现监控视频人数统计
2021/05/21 Python
MySQL池化框架学习接池自定义
2022/07/23 MySQL