⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 changecode.inc.php

📁 功能齐全
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?php

/**
 * 中文编码集合类库
 *
 * 目前该类库可以实现,简体中文 <-> 繁体中文编码互换,简体中文、繁体中文 -> 拼音单向转换,
 * 简体中文、繁体中文 <-> UTF8 编码转换,简体中文、繁体中文 -> Unicode单向转换
 *
 * @作者         Hessian(solarischan@21cn.com)
 * @版本         1.5
 * @版权所有     Hessian / NETiS
 * @使用授权     GPL(不能应用于任何商业用途,无须经过作者同意即可修改代码,但修改后的代码必须按照GPL协议发布)
 * @特别鸣谢     unknow(繁简转换代码片断)
 * @起始         2003-04-01
 * @最后修改     2003-06-06
 * @访问         公开
 *
 * 更新记录
 *
 * ver 1.5 2003-06-06
 * 增加 UTF8 转换到 GB2312、BIG5的功能。
 *
 * ver 1.4 2003-04-07
 * 增加 当转换HTML时设定为true,即可改变charset的值。
 *
 * ver 1.3 2003-04-02
 * 增加 繁体中文转换至拼音的功能。
 *
 * ver 1.2 2003-04-02
 * 合并 简体、繁体中文转换至UTF8的函数。
 * 修改 简体中文转换至拼音的函数,返回值更改为字符串,每一个汉字的拼音用空格分开
 * 增加 简体中文转换为 UNICODE 的功能。
 * 增加 繁体中文转换为 UNICODE 的功能。
 *
 * ver 1.1 2003-04-02
 * 增加 OpenFile() 函数,支持打开本地文件和远程文件。
 * 增加 简体中文转换为 UTF8 的功能。
 * 增加 繁体中文转换为 UTF8 的功能。
 *
 * ver 1.0 2003-04-01
 * 一个集合了中文简体,中文繁体对应各种编码互换的类库已经初步完成。
 */

if(!defined("__CLASS_CHANGECODE__"))
{
	define("__CLASS_CHANGECODE__",1);

	class ChangeCode
	{

		/**
    	 * 存放简体中文与拼音对照表
    	 *
    	 * @变量类型  数组
    	 * @起始      1.0
    	 * @最后修改  1.0
    	 * @访问      内部
    	 */
		var $pinyin_table = array();


		/**
    	 * 存放 GB <-> UNICODE 对照表的内容
    	 * @变量类型
    	 * @起始      1.1
    	 * @最后修改  1.2
    	 * @访问      内部
    	 */
		var $unicode_table = array();

		/**
    	 * 访问中文繁简互换表的文件指针
    	 *
    	 * @变量类型  对象
    	 * @起始      1.0
    	 * @最后修改  1.0
    	 * @访问      内部
    	 */
		var $ctf;

		/**
    	 * 等待转换的字符串
    	 * @变量类型
    	 * @起始      1.0
    	 * @最后修改  1.0
    	 * @访问      内部
    	 */
		var $SourceText = "";

		var $codetable_dir ; //  存放各种语言互换表的目录

		/**
    	 * Chinese 的运行配置
    	 *
    	 * @变量类型  数组
    	 * @起始      1.0
    	 * @最后修改  1.2
    	 * @访问      公开
    	 */
		var $config  =  array(
		'SourceLang'            => '',                    //  字符的原编码
		'TargetLang'            => '',                    //  转换后的编码
		'GBtoBIG5_table'        => 'gb-big5.table',       //  简体中文转换为繁体中文的对照表
		'BIG5toGB_table'        => 'big5-gb.table',       //  繁体中文转换为简体中文的对照表
		'GBtoPinYin_table'      => 'gb-pinyin.table',     //  简体中文转换为拼音的对照表
		'GBtoUnicode_table'     => 'gb-unicode.table',    //  简体中文转换为UNICODE的对照表
		'BIG5toUnicode_table'   => 'big5-unicode.table'   //  繁体中文转换为UNICODE的对照表
		);

		/**
    	 * Chinese 的悉构函数
    	 *
    	 * 详细说明
    	 * @形参      字符串 $SourceLang 为需要转换的字符串的原编码
    	 *            字符串 $TargetLang 为转换的目标编码
    	 *            字符串 $SourceText 为等待转换的字符串
    	 *
    	 * @起始      1.0
    	 * @最后修改  1.2
    	 * @访问      公开
    	 * @返回值    无
    	 * @throws
    	 */
		function ChangeCode( $SourceLang , $TargetLang , $SourceString='')
		{
			$this->codetable_dir =  dirname(__FILE__) . "/codetable/";

			if ($SourceLang != '') {
				$this->config['SourceLang'] = $SourceLang;
			}

			if ($TargetLang != '') {
				$this->config['TargetLang'] = $TargetLang;
			}

			if ($SourceString != '') {
				$this->SourceText = $SourceString;
			}

			$this->OpenTable();
		} // 结束 Chinese 的悉构函数


		/**
    	 * 将 16 进制转换为 2 进制字符
    	 *
    	 * 详细说明
    	 * @形参      $hexdata 为16进制的编码
    	 * @起始      1.5
    	 * @最后修改  1.5
    	 * @访问      内部
    	 * @返回      字符串
    	 * @throws
    	 */
		function _hex2bin( $hexdata )
		{
			for ( $i=0; $i<strlen($hexdata); $i+=2 )
			$bindata.=chr(hexdec(substr($hexdata,$i,2)));

			return $bindata;
		}


		/**
    	 * 打开对照表
    	 *
    	 * 详细说明
    	 * @形参
    	 * @起始      1.3
    	 * @最后修改  1.3
    	 * @访问      内部
    	 * @返回      无
    	 * @throws
    	 */
		function OpenTable()
		{

			// 假如原编码为简体中文的话
			if ($this->config['SourceLang']=="GB2312") {

				// 假如转换目标编码为繁体中文的话
				if ($this->config['TargetLang'] == "BIG5") {
					$this->ctf = fopen($this->codetable_dir.$this->config['GBtoBIG5_table'], "r");
					if (is_null($this->ctf)) {
						echo "打开打开转换表文件失败!";
						exit;
					}
				}

				// 假如转换目标编码为拼音的话
				if ($this->config['TargetLang'] == "PinYin") {
					$tmp = @file($this->codetable_dir.$this->config['GBtoPinYin_table']);
					if (!$tmp) {
						echo "打开打开转换表文件失败!";
						exit;
					}
					//
					$i = 0;
					for ($i=0; $i<count($tmp); $i++) {
						$tmp1 = explode("	", $tmp[$i]);
						$this->pinyin_table[$i]=array($tmp1[0],$tmp1[1]);
					}
				}

				// 假如转换目标编码为 UTF8 的话
				if ($this->config['TargetLang'] == "UTF8") {
					$tmp = @file($this->codetable_dir.$this->config['GBtoUnicode_table']);
					if (!$tmp) {
						echo "打开打开转换表文件失败!";
						exit;
					}
					$this->unicode_table = array();
					while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,0,6))]=substr($value,7,6);
				}

				// 假如转换目标编码为 UNICODE 的话
				if ($this->config['TargetLang'] == "UNICODE") {
					$tmp = @file($this->codetable_dir.$this->config['GBtoUnicode_table']);
					if (!$tmp) {
						echo "打开打开转换表文件失败!";
						exit;
					}
					$this->unicode_table = array();
					while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,0,6))]=substr($value,9,4);
				}
			}

			// 假如原编码为繁体中文的话
			if ($this->config['SourceLang']=="BIG5") {
				// 假如转换目标编码为简体中文的话
				if ($this->config['TargetLang'] == "GB2312") {
					$this->ctf = fopen($this->codetable_dir.$this->config['BIG5toGB_table'], "r");
					if (is_null($this->ctf)) {
						echo "打开打开转换表文件失败!";
						exit;
					}
				}
				// 假如转换目标编码为 UTF8 的话
				if ($this->config['TargetLang'] == "UTF8") {
					$tmp = @file($this->codetable_dir.$this->config['BIG5toUnicode_table']);
					if (!$tmp) {
						echo "打开打开转换表文件失败!";
						exit;
					}
					$this->unicode_table = array();
					while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,0,6))]=substr($value,7,6);
				}

				// 假如转换目标编码为 UNICODE 的话
				if ($this->config['TargetLang'] == "UNICODE") {
					$tmp = @file($this->codetable_dir.$this->config['BIG5toUnicode_table']);
					if (!$tmp) {
						echo "打开打开转换表文件失败!";
						exit;
					}
					$this->unicode_table = array();
					while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,0,6))]=substr($value,9,4);
				}

				// 假如转换目标编码为拼音的话
				if ($this->config['TargetLang'] == "PinYin") {
					$tmp = @file($this->codetable_dir.$this->config['GBtoPinYin_table']);
					if (!$tmp) {
						echo "打开打开转换表文件失败!";
						exit;
					}
					//
					$i = 0;
					for ($i=0; $i<count($tmp); $i++) {
						$tmp1 = explode("	", $tmp[$i]);
						$this->pinyin_table[$i]=array($tmp1[0],$tmp1[1]);
					}
				}
			}

			// 假如原编码为 UTF8 的话
			if ($this->config['SourceLang']=="UTF8") {

				// 假如转换目标编码为 GB2312 的话
				if ($this->config['TargetLang'] == "GB2312") {
					$tmp = @file($this->codetable_dir.$this->config['GBtoUnicode_table']);
					if (!$tmp) {
						echo "打开打开转换表文件失败!";
						exit;
					}
					$this->unicode_table = array();
					while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,7,6))]=substr($value,0,6);
				}

				// 假如转换目标编码为 BIG5 的话
				if ($this->config['TargetLang'] == "BIG5") {
					$tmp = @file($this->codetable_dir.$this->config['BIG5toUnicode_table']);
					if (!$tmp) {
						echo "打开打开转换表文件失败!";
						exit;
					}
					$this->unicode_table = array();
					while(list($key,$value)=each($tmp))
					$this->unicode_table[hexdec(substr($value,7,6))]=substr($value,0,6);
				}
			}

		} // 结束 OpenTable 函数

		/**
    	 * 打开本地或者远程的文件
    	 *
    	 * 详细说明
    	 * @形参      字符串 $position 为需要打开的文件名称,支持带路径或URL
    	 *            布尔值 $isHTML 为待转换的文件是否为html文件
    	 * @起始      1.1
    	 * @最后修改  1.1
    	 * @访问      公开
    	 * @返回      无
    	 * @throws
    	 */
		function OpenFile( $position , $isHTML=false )
		{
			$tempcontent = @file($position);

			if (!$tempcontent) {
				echo "打开文件失败!";
				exit;
			}

			$this->SourceText = implode("",$tempcontent);

			if ($isHTML) {
				$this->SourceText = eregi_replace( "charset=".$this->config['SourceLang'] , "charset=".$this->config['TargetLang'] , $this->SourceText);

				$this->SourceText = eregi_replace("\n", "", $this->SourceText);

				$this->SourceText = eregi_replace("\r", "", $this->SourceText);
			}
		} // 结束 OpenFile 函数

		/**
    	 * 打开本地或者远程的文件
    	 *
    	 * 详细说明
    	 * @形参      字符串 $position 为需要打开的文件名称,支持带路径或URL
    	 * @起始      1.1
    	 * @最后修改  1.1
    	 * @访问      公开
    	 * @返回      无
    	 * @throws
    	 */
		function SiteOpen( $position )
		{
			$tempcontent = @file($position);

			if (!$tempcontent) {
				echo "打开文件失败!";
				exit;
			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -