php实现twTrCn-简繁转化

   参照别人的PHP方法,封装了一个PHP简繁体转化的类。
其中包括一个配置文件、一个类文件。
配置文件:主要是简繁体对应的字体,可以手动的添加简繁体库
类文件:主要是两个function,一个提供简体转化为繁体,相应的另外一个就是繁体转化为简体。
这里的配置文件我就不解释了,可以看一下转化类的代码:
require_once "transfer_config.php";//读取简繁体配置文件
class Transfer {
const ZH_ASCII_LOW = 224; //中文ASCII的最小值
const ZH_ASCII_HIGHT = 239; //中文ASCII的最大值
public static $utf8_gb2312; //类的变量,存储繁体
public static $utf8_big5; //类的变量,存储简体
public function __construct() {
global $UTF8_GB2312;
global $UTF8_BIG5;
self::$utf8_gb2312 = $UTF8_GB2312;
self::$utf8_big5 = $UTF8_BIG5;
}
public function cnToTw($string) {
$traditional = ""; //繁体变量初始化
$length = strlen($string); //提取string字符串的ASCII码长度,注意一个中文字符对应于3位
$count = 0;
while ($count < $length) {
//获取$count位置上的ASCII值
$ascii = ord($string{$count});
if ($ascii >= self::ZH_ASCII_LOW && $ascii <= self::ZH_ASCII_HIGHT) {
if (($temp = strpos(self::$utf8_gb2312, $string{$count} . $string{$count + 1} . $string{$count + 2})) !== false) {
$traditional .= self::$utf8_big5{$temp} . self::$utf8_big5{$temp + 1} . self::$utf8_big5{$temp + 2};
$count += 3;
continue;
}
}
$traditional .= $string{$count++};
}
return $traditional;
}
public function twToCn($string) {
$simplified = "";
$length = strlen($string);
$count = 0;
while ($count < $length) {
//获取$count位置上的ASCII值
$ascii = ord($string{$count});
if ($ascii >= self::ZH_ASCII_LOW && $ascii <= self::ZH_ASCII_HIGHT) {
if (($temp = strpos(self::$utf8_big5, $string{$count} . $string{$count + 1} . $string{$count + 2})) !== false) {
$simplified .= self::$utf8_gb2312{$temp} . self::$utf8_gb2312{$temp + 1} . self::$utf8_gb2312{$temp + 2};
$count += 3;
continue;
}
}
$simplified .= $string{$count++};
}
return $simplified;
}
}


     配有附件测试代码,需要的话可以下载使用哦亲!

转载于:https://www.cnblogs.com/huangdh3/archive/2011/11/05/2237194.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
序文 I. 入门指引 1. 简介 2. 简明教程 3. 安装 4. 运行时配置 II. 语言参考 5. 基本语法 6. 类型 7. 变量 8. 常量 9. 表达式 10. 运算符 11. 流程控制 12. 函数 13. 类与对象 14. 引用的解释 III. 安全 15. 安全 IV. 特点 16. 用 PHP 进行 HTTP 认证 17. Cookies 18. 文件上传处理 19. 使用远程文件 20. 连接处理 21. 数据库永久连接 22. 安全模式 23. PHP 的命令行模式 V. 函数参考 I. Apache 函数库 II. 数组函数库 III. Aspell functions [deprecated] IV. BCMath Arbitrary Precision Mathematics Functions V. Bzip2 Compression Functions VI. Calendar Functions VII. CCVS API Functions [deprecated] VIII. Windows 的 COM 支持函数库 IX. 类/对象函数库 X. ClibPDF Functions XI. Crack Functions XII. CURL, Client URL Library Functions XIII. Cybercash Payment Functions XIV. Cyrus IMAP administration Functions XV. Character Type Functions XVI. Database (dbm-style) Abstraction Layer Functions XVII. 时间日期函数库 XVIII. dBase Functions XIX. DBM Functions [deprecated] XX. dbx Functions XXI. DB++ Functions XXII. Direct IO Functions XXIII. 目录函数库 XXIV. DOM XML Functions XXV. .NET Functions XXVI. Error Handling and Logging Functions XXVII. File Alteration Monitor Functions XXVIII. FrontBase Functions XXIX. filePro Functions XXX. 文件系统函数库 XXXI. Forms Data Format Functions XXXII. FriBiDi Functions XXXIII. FTP 函数库 XXXIV. Function Handling Functions XXXV. Gettext XXXVI. GMP Functions XXXVII. HTTP 相关函数库 XXXVIII. Hyperwave Functions XXXIX. Hyperwave API Functions XL. iconv Functions XLI. 图像函数库 XLII. IMAP, POP3 and NNTP Functions XLIII. Informix Functions XLIV. InterBase Functions XLV. Ingres II Functions XLVI. IRC Gateway Functions XLVII. PHP / Java Integration XLVIII. LDAP Functions XLIX. LZF Functions L. Mail Functions LI. mailparse Functions LII. 数学函数库 LIII. Multi-Byte String Functions LIV. MCAL Functions LV. Mcrypt Encryption Functions LVI. MCVE Payment Functions LVII. Mhash Functions LVIII. Mimetype Functions LIX. Microsoft SQL Server Functions LX. Ming functions for Flash LXI. Miscellaneous Functions LXII. mnoGoSearch Functions LXIII. mSQL Functions LXIV. MySQL 函数库 LXV. Improved MySQL Extension LXVI. Mohawk Software Session Handler Functions LXVII. muscat Functions LXVIII. Network Functions LXIX. Ncurses Terminal Screen Control Functions LXX. Lotus Notes Functions LXXI. NSAPI-specific Functions LXXII. Unified ODBC Functions LXXIII. Object Aggregation/Composition Functions LXXIV. Oracle 8 函数库 LXXV. OpenSSL Functions LXXVI. Oracle 函数库 LXXVII. Ovrimos SQL Functions LXXVIII. 输出控制函数 LXXIX. Object property and method call overloading LXXX. PDF functions LXXXI. Verisign Payflow Pro Functions LXXXII. PHP Options&Information LXXXIII. POSIX Functions LXXXIV. PostgreSQL 数据库函数库 LXXXV. Process Control Functions LXXXVI. Program Execution Functions LXXXVII. Printer Functions LXXXVIII. Pspell Functions LXXXIX. GNU Readline XC. GNU Recode Functions XCI. 正则表达式函数库(Perl 兼容) XCII. qtdom Functions XCIII. 正则表达式函数库(POSIX 扩展) XCIV. Semaphore, Shared Memory and IPC Functions XCV. SESAM Database Functions XCVI. Session Handling Functions XCVII. Shared Memory Functions XCVIII. SimpleXML functions XCIX. SOAP Functions C. SQLite CI. Shockwave Flash Functions CII. SNMP 函数库 CIII. Socket Functions CIV. Standard PHP Library (SPL) Functions CV. Stream Functions CVI. 字符串处理函数库 CVII. Sybase Functions CVIII. TCP Wrappers Functions CIX. Tidy Functions CX. Tokenizer Functions CXI. URL 函数库 CXII. 变量函数库 CXIII. vpopmail Functions CXIV. W32api 函数库 CXV. WDDX Functions CXVI. XML 语法解析函数库 CXVII. XML-RPC 函数库 CXVIII. xdiff Functions CXIX. XSLT Functions CXX. YAZ Functions CXXI. YP/NIS Functions CXXII. Zip File Functions (Read Only Access) CXXIII. Zlib Compression Functions VI. Zend API 24. Overview 25. Extension Possibilities 26. Source Layout 27. PHP's Automatic Build System 28. Creating Extensions 29. Using Extensions 30. Troubleshooting 31. Source Discussion 32. Accepting Arguments 33. Creating Variables 34. Duplicating Variable Contents: The Copy Constructor 35. Returning Values 36. Printing Information 37. Startup and Shutdown Functions 38. Calling User Functions 39. Initialization File Support 40. Where to Go from Here 41. Reference: Some Configuration Macros 42. API Macros VII. PHP API: 扩展开发接口 43. Streams API for PHP Extension Authors VIII. FAQ: 常见问题 44. 一般信息 45. 邮件列表 46. 获取 PHP 47. 数据库问题 48. 安装 49. 编译问题 50. 使用 PHP 51. PHP 和 HTML 52. PHP 和 COM 53. PHP 和其它语言 54. 从 PHP/FI 2 移植到 PHP 3 55. 从 PHP 3 移植到 PHP 4 56. 杂类问题 IX. 附录 A. PHP 及其相关工程的历史 B. Migrating from PHP 4 to PHP 5 C. 从 PHP 3 移植到 PHP 4 D. 从 PHP/FI 2 移植到 PHP 3 E. PHP 的调试 F. 扩展 PHP 3 G. 函数别名列表 H. 保留字列表 I. 资源类型列表 J. 支持的协议/封装协议列表 K. 所支持的套接字传输器(Socket Transports)列表 L. PHP 类型及比较符表 M. 解析器代号列表 N. 关于本手册 O. Open Publication License P. 函数索引 Q. 补充说明 X. 微软 HTML 帮助版本 57. 关于此版本 58. Using PHP Manual CHM Edition 59. The Full Text Search 60. Specialities of this Edition 61. Integrating the PHP Manual 62. Skin development 63. CHM Edition Credits
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值