📄 class.chinese.php
字号:
$tempcontent = @file($position);
if (!$tempcontent) {
echo "打开文件失败!";
exit;
}
// 将数组的所有内容转换为字符串
$this->SourceText = implode("",$tempcontent);
$this->SourceText = eregi_replace( "charset=".$this->config['SourceLang'] , "charset=".$this->config['TargetLang'] , $this->SourceText);
// ereg(href="css/dir.css"
} // 结束 OpenFile 函数
/**
* 设置变量的值
*
* 详细说明
* @形参
* @起始 1.0
* @最后修改 1.0
* @访问 公开
* @返回值 无
* @throws
*/
function setvar( $parameter , $value )
{
if(!trim($parameter))
return $parameter;
$this->config[$parameter] = $value;
} // 结束 setvar 函数
/**
* 将简体、繁体中文的 UNICODE 编码转换为 UTF8 字符
*
* 详细说明
* @形参 数字 $c 简体中文汉字的UNICODE编码的10进制
* @起始 1.1
* @最后修改 1.2
* @访问 内部
* @返回 字符串
* @throws
*/
function CHSUtoUTF8($c)
{
$str="";
if ($c < 0x80) {
$str.=$c;
}
else if ($c < 0x800) {
$str.=(0xC0 | $c>>6);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x10000) {
$str.=(0xE0 | $c>>12);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x200000) {
$str.=(0xF0 | $c>>18);
$str.=(0x80 | $c>>12 & 0x3F);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
return $str;
} // 结束 CHSUtoUTF8 函数
/**
* 简体、繁体中文 <-> UTF8 互相转换的函数
*
* 详细说明
* @形参
* @起始 1.1
* @最后修改 1.5
* @访问 内部
* @返回 字符串
* @throws
*/
function CHStoUTF8(){
if ($this->config["SourceLang"]=="BIG5" || $this->config["SourceLang"]=="GB2312") {
$ret="";
while($this->SourceText != ""){
if(ord(substr($this->SourceText,0,1))>127){
if ($this->config["SourceLang"]=="BIG5") {
$utf8=$this->CHSUtoUTF8(hexdec($this->unicode_table[hexdec(bin2hex(substr($this->SourceText,0,2)))]));
}
if ($this->config["SourceLang"]=="GB2312") {
$utf8=$this->CHSUtoUTF8(hexdec($this->unicode_table[hexdec(bin2hex(substr($this->SourceText,0,2)))-0x8080]));
}
for($i=0;$i<strlen($utf8);$i+=3)
$ret.=chr(substr($utf8,$i,3));
$this->SourceText=substr($this->SourceText,2,strlen($this->SourceText));
}
else{
$ret.=substr($this->SourceText,0,1);
$this->SourceText=substr($this->SourceText,1,strlen($this->SourceText));
}
}
$this->unicode_table = array();
$this->SourceText = "";
return $ret;
}
if ($this->config["SourceLang"]=="UTF8") {
$out = "";
$len = strlen($this->SourceText);
$i = 0;
while($i < $len) {
$c = ord( substr( $this->SourceText, $i++, 1 ) );
switch($c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
$out .= substr( $this->SourceText, $i-1, 1 );
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
$char2 = ord( substr( $this->SourceText, $i++, 1 ) );
$char3 = $this->unicode_table[(($c & 0x1F) << 6) | ($char2 & 0x3F)];
if ($this->config["TargetLang"]=="GB2312")
$out .= $this->_hex2bin( dechex( $char3 + 0x8080 ) );
if ($this->config["TargetLang"]=="BIG5")
$out .= $this->_hex2bin( $char3 );
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
$char2 = ord( substr( $this->SourceText, $i++, 1 ) );
$char3 = ord( substr( $this->SourceText, $i++, 1 ) );
$char4 = $this->unicode_table[(($c & 0x0F) << 12) | (($char2 & 0x3F) << 6) | (($char3 & 0x3F) << 0)];
if ($this->config["TargetLang"]=="GB2312")
$out .= $this->_hex2bin( dechex ( $char4 + 0x8080 ) );
if ($this->config["TargetLang"]=="BIG5")
$out .= $this->_hex2bin( $char4 );
break;
}
}
// 返回结果
return $out;
}
} // 结束 CHStoUTF8 函数
/**
* 简体、繁体中文转换为 UNICODE编码
*
* 详细说明
* @形参
* @起始 1.2
* @最后修改 1.2
* @访问 内部
* @返回 字符串
* @throws
*/
function CHStoUNICODE()
{
$utf="";
while($this->SourceText != "")
{
if (ord(substr($this->SourceText,0,1))>127)
{
if ($this->config["SourceLang"]=="GB2312")
$utf.="&#x".$this->unicode_table[hexdec(bin2hex(substr($this->SourceText,0,2)))-0x8080].";";
if ($this->config["SourceLang"]=="BIG5")
$utf.="&#x".$this->unicode_table[hexdec(bin2hex(substr($this->SourceText,0,2)))].";";
$this->SourceText=substr($this->SourceText,2,strlen($this->SourceText));
}
else
{
$utf.=substr($this->SourceText,0,1);
$this->SourceText=substr($this->SourceText,1,strlen($this->SourceText));
}
}
return $utf;
} // 结束 CHStoUNICODE 函数
/**
* 简体中文 <-> 繁体中文 互相转换的函数
*
* 详细说明
* @起始 1.0
* @访问 内部
* @返回值 经过编码的utf8字符
* @throws
*/
function GB2312toBIG5()
{
// 获取等待转换的字符串的总长度
$max=strlen($this->SourceText)-1;
for($i=0;$i<$max;$i++){
$h=ord($this->SourceText[$i]);
if($h>=160){
$l=ord($this->SourceText[$i+1]);
if($h==161 && $l==64){
$gb=" ";
}
else{
fseek($this->ctf,($h-160)*510+($l-1)*2);
$gb=fread($this->ctf,2);
}
$this->SourceText[$i]=$gb[0];
$this->SourceText[$i+1]=$gb[1];
$i++;
}
}
fclose($this->ctf);
// 将转换后的结果赋予 $result;
$result = $this->SourceText;
// 清空 $thisSourceText
$this->SourceText = "";
// 返回转换结果
return $result;
} // 结束 GB2312toBIG5 函数
/**
* 根据所得到的编码搜寻拼音
*
* 详细说明
* @起始 1.0
* @最后修改 1.0
* @访问 内部
* @返回值 字符串
* @throws
*/
function PinYinSearch($num){
if($num>0&&$num<160){
return chr($num);
}
elseif($num<-20319||$num>-10247){
return "";
}
else{
for($i=count($this->pinyin_table)-1;$i>=0;$i--){
if($this->pinyin_table[$i][1]<=$num)
break;
}
return $this->pinyin_table[$i][0];
}
} // 结束 PinYinSearch 函数
/**
* 简体、繁体中文 -> 拼音 转换
*
* 详细说明
* @起始 1.0
* @最后修改 1.3
* @访问 内部
* @返回值 字符串,每个拼音用空格分开
* @throws
*/
function CHStoPinYin(){
if ( $this->config['SourceLang']=="BIG5" ) {
$this->ctf = fopen($this->config['codetable_dir'].$this->config['BIG5toGB_table'], "r");
if (is_null($this->ctf)) {
echo "打开打开转换表文件失败!";
exit;
}
$this->SourceText = $this->GB2312toBIG5();
$this->config['TargetLang'] = "PinYin";
}
$ret = array();
$ri = 0;
for($i=0;$i<strlen($this->SourceText);$i++){
$p=ord(substr($this->SourceText,$i,1));
if($p>160){
$q=ord(substr($this->SourceText,++$i,1));
$p=$p*256+$q-65536;
}
$ret[$ri]=$this->PinYinSearch($p);
$ri = $ri + 1;
}
// 清空 $this->SourceText
$this->SourceText = "";
$this->pinyin_table = array();
// 返回转换后的结果
return implode(" ", $ret);
} // 结束 CHStoPinYin 函数
/**
* 输出转换结果
*
* 详细说明
* @形参
* @起始 1.0
* @最后修改 1.2
* @访问 公开
* @返回 字符换
* @throws
*/
function ConvertIT()
{
// 判断是否为中文繁、简转换
if ( ($this->config['SourceLang']=="GB2312" || $this->config['SourceLang']=="BIG5") && ($this->config['TargetLang']=="GB2312" || $this->config['TargetLang']=="BIG5") ) {
return $this->GB2312toBIG5();
}
// 判断是否为简体中文与拼音转换
if ( ($this->config['SourceLang']=="GB2312" || $this->config['SourceLang']=="BIG5") && $this->config['TargetLang']=="PinYin" ) {
return $this->CHStoPinYin();
}
// 判断是否为简体、繁体中文与UTF8转换
if ( ($this->config['SourceLang']=="GB2312" || $this->config['SourceLang']=="BIG5" || $this->config['SourceLang']=="UTF8") && ($this->config['TargetLang']=="UTF8" || $this->config['TargetLang']=="GB2312" || $this->config['TargetLang']=="BIG5") ) {
return $this->CHStoUTF8();
}
// 判断是否为简体、繁体中文与UNICODE转换
if ( ($this->config['SourceLang']=="GB2312" || $this->config['SourceLang']=="BIG5") && $this->config['TargetLang']=="UNICODE" ) {
return $this->CHStoUNICODE();
}
} // 结束 ConvertIT 函数
} // 结束类库
/**
*/
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -