📄 doiconv.php
字号:
<?php
class Chinese
{
//存放简体中文与拼音对照表
var $pinyin_table = array();
//存放 GB <-> UNICODE 对照表的内容
var $unicode_table = array();
//访问中文繁简互换表的文件指针
var $ctf;
var $SourceText = "";
//配置
var $config = array(
'codetable_dir' => '', // 存放各种语言互换表的目录
'source_lang' => '', // 字符的原编码
'target_lang' => '', // 转换后的编码
'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的对照表
);
function Chinese($dir='./')
{
$this->config['codetable_dir'] = $dir."../data/codetable/";
}
function Convert( $source_lang , $target_lang , $source_string='' )
{
/* 如果编码相同,直接返回 */
if ($source_lang == $target_lang || $source_string == '')
{
return $source_string;
}
if ($source_lang != '') {
$this->config['source_lang'] = $source_lang;
}
if ($target_lang != '') {
$this->config['target_lang'] = $target_lang;
}
$this->SourceText = $source_string;
$this->OpenTable();
// 判断是否为中文繁、简转换
if ( ($this->config['source_lang']=="GB2312" || $this->config['source_lang']=="BIG5") && ($this->config['target_lang']=="GB2312" || $this->config['target_lang']=="BIG5") ) {
return $this->GB2312toBIG5();
}
// 判断是否为简体中文与拼音转换
if ( ($this->config['source_lang']=="GB2312" || $this->config['source_lang']=="BIG5") && $this->config['target_lang']=="PinYin" ) {
return $this->CHStoPinYin();
}
// 判断是否为简体、繁体中文与UTF8转换
if ( ($this->config['source_lang']=="GB2312" || $this->config['source_lang']=="BIG5" || $this->config['source_lang']=="UTF8") && ($this->config['target_lang']=="UTF8" || $this->config['target_lang']=="GB2312" || $this->config['target_lang']=="BIG5") ) {
return $this->CHStoUTF8();
}
// 判断是否为简体、繁体中文与UNICODE转换
if ( ($this->config['source_lang']=="GB2312" || $this->config['source_lang']=="BIG5") && $this->config['target_lang']=="UNICODE" ) {
return $this->CHStoUNICODE();
}
}
//将 16 进制转换为 2 进制字符
function _hex2bin( $hexdata )
{
$bindata = '';
for ($i = 0; $i < strlen($hexdata); $i += 2 )
{
$bindata .= chr(hexdec(substr($hexdata, $i, 2)));
}
return $bindata;
}
function OpenTable()
{
// 假如原编码为简体中文的话
if ($this->config['source_lang']=="GB2312") {
// 假如转换目标编码为繁体中文的话
if ($this->config['target_lang'] == "BIG5") {
$this->ctf = fopen($this->config['codetable_dir'].$this->config['GBtoBIG5_table'], "rb");
if (is_null($this->ctf)) {
echo "打开打开转换表文件失败!";
exit;
}
}
// 假如转换目标编码为拼音的话
if ($this->config['target_lang'] == "PinYin") {
$tmp = @file($this->config['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['target_lang'] == "UTF8") {
$tmp = @file($this->config['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['target_lang'] == "UNICODE") {
$tmp = @file($this->config['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['source_lang']=="BIG5") {
// 假如转换目标编码为简体中文的话
if ($this->config['target_lang'] == "GB2312") {
$this->ctf = fopen($this->config['codetable_dir'].$this->config['BIG5toGB_table'], "r");
if (is_null($this->ctf)) {
echo "打开打开转换表文件失败!";
exit;
}
}
// 假如转换目标编码为 UTF8 的话
if ($this->config['target_lang'] == "UTF8") {
$tmp = @file($this->config['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['target_lang'] == "UNICODE") {
$tmp = @file($this->config['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['target_lang'] == "PinYin") {
$tmp = @file($this->config['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['source_lang']=="UTF8") {
// 假如转换目标编码为 GB2312 的话
if ($this->config['target_lang'] == "GB2312") {
$tmp = @file($this->config['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['target_lang'] == "BIG5") {
$tmp = @file($this->config['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);
}
}
}
}
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['source_lang'] , "charset=".$this->config['target_lang'] , $this->SourceText);
$this->SourceText = eregi_replace("\n", "", $this->SourceText);
$this->SourceText = eregi_replace("\r", "", $this->SourceText);
}
}
function SiteOpen( $position )
{
$tempcontent = @file($position);
if (!$tempcontent) {
echo "打开文件失败!";
exit;
}
// 将数组的所有内容转换为字符串
$this->SourceText = implode("",$tempcontent);
$this->SourceText = eregi_replace( "charset=".$this->config['source_lang'] , "charset=".$this->config['target_lang'] , $this->SourceText);
}
function setvar( $parameter , $value )
{
if(!trim($parameter))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -