Hi-Fi優質I.T網:PHP嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙?forum_id=51-討論區
您尚未登入...
帳號:
密碼:

忘記密碼 
 
  訂閱電子報

取消   訂閱
 
討論區>>PHP嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙踝蕭嚙?forum_id=51<<

 
 
發起人:
ruby
一般會員
張貼時間:2008/11/10-13:25:09
   檢舉

 主題:iconv(utf8 -gb2312-utf8)

<?php
if(false){
?>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<meta http-equiv="Content-Language" content="zh-tw">
<?php
}


class PIconv{


var $fpB2G = NULL;
var $fpG2B = NULL;


function PIconv(){
}


function Free(){
if(isset($this->fpB2G)){
fclose($this->fpB2G);
$this->fpB2G = NULL;
}
if(isset($this->fpG2B)){
fclose($this->fpG2B);
$this->fpG2B = NULL;
}
unset($this);
}


function FillBnG(&$AstrBig5,&$AstrGB2312,$AdbBig5 = NULL,$AdbGB2312 = NULL){
if((string)$AstrBig5 != ''
&& (string)$AstrGB2312 == ''){
$AstrGB2312 = $this->Big5ToGB2312($AstrBig5);
}else if((string)$AstrGB2312 != ''
&& (string)$AstrBig5 == ''){
$AstrBig5 = $this->GB2312ToBig5($AstrGB2312);
}else if((string)$AstrBig5 != ''
&& (string)$AstrGB2312 != ''){
if("$AdbBig5" != '' && (string)$AstrBig5 != "$AdbBig5" && "$AdbGB2312" != '' && (string)$AstrGB2312 != "$AdbGB2312"){
}else if("$AdbBig5" != '' && (string)$AstrBig5 != "$AdbBig5"){
$AstrGB2312 = $this->Big5ToGB2312($AstrBig5);
}else if("$AdbGB2312" != '' && (string)$AstrGB2312 != "$AdbGB2312"){
$AstrBig5 = $this->GB2312ToBig5($AstrGB2312);
}
}
}


function Big5ToGB2312($Astr){
Global $Application;
if($Application->IsUTF8()){
$big5str = $this->IConv('UTF-8','Big5',$Astr);
if($big5str === false) return '';
$gbstr = $this->B2G($big5str);
$Areturnstr = $this->IConv('GB2312','UTF-8',$gbstr);
if($Areturnstr === false) return '';
}else{
$Areturnstr = $this->B2G($Astr);
}
return $Areturnstr;
}


function GB2312ToBig5($Astr){
Global $Application;
if($Application->IsUTF8()){
$gbstr = $this->IConv('UTF-8','GB2312',$Astr);
if($gbstr === false) return '';
$big5str = $this->G2B($gbstr);
$Areturnstr = $this->IConv('Big5','UTF-8',$big5str);
if($Areturnstr === false) return '';
}else{
$Areturnstr = $this->G2B($Astr);
}
return $Areturnstr;
}


function IConv($aacp1,$Aacp2,$Astr1){
if("$aacp1" != 'UTF-8'){
$Astr2 = @iconv($aacp1,$Aacp2,$Astr1);
return $Astr2;
}
$str2 = '';
$j = strlen($Astr1);
for($i = 0;$i < $j;$i++){
$ascstr = substr($Astr1,$i,1);
$ordasc = ord($ascstr);
if(($ordasc >= 240) && ($ordasc <= 255)){
$l = 4;
}else if(($ordasc >= 224) && ($ordasc <= 239)){
$l = 3;
}else if(($ordasc >= 192) && ($ordasc <= 223)){
$l = 2;
}else{
$l = 1;
}
$asc1 = substr($Astr1,$i,$l);
$asc2 = @iconv($aacp1,$Aacp2,$asc1);
if("$asc2" == ''){
$asc2 = '';
for($k = 0;$k < $l;$k++){
$asc2 .= '?';
}
}
$str2 .= $asc2;
$i += ($l - 1);
}
return $str2;
}


function B2G($Astr){
Global $Application;
if(!$this->fpB2G) $this->fpB2G = fopen($Application->PhpObjectDir.'iconv/big5togb.dat','r');
$max = strlen($Astr);
for($i = 0;$i < $max;$i++){
$h = ord($Astr[$i]);
if((($i + 1) < $max) && ($h >= 160)){
$l = ord($Astr[$i + 1]);
if ($h == 161 && $l == 64) {
$gb = ' ';
} else {
fseek($this->fpB2G,($h - 160) * 510 + ($l - 1) * 2);
$gb = fread($this->fpB2G,2);
}
$Astr[$i] = $gb[0];
$Astr[$i + 1] = $gb[1];
$i++;
}
}
return $Astr;
}


function G2B($Astr){
Global $Application;
if(!$this->fpG2B) $this->fpG2B = fopen($Application->PhpObjectDir.'iconv/gb2big5.dat','r');
$max = strlen($Astr);
for ($i = 0;$i < $max;$i++) {
$h = ord($Astr[$i]);
if((($i + 1)) < $max && ($h >= 160)) {
$l = ord($Astr[$i + 1]);
if ($h == 161 && $l == 64) {
$gb = ' ';
} else {
fseek($this->fpG2B,($h - 160) * 510 + ($l - 1) * 2);
$gb = fread($this->fpG2B,2);
}
$Astr[$i] = $gb[0];
$Astr[$i + 1] = $gb[1];
$i++;
}
}
return $Astr;
}
}


簽名
By Ruby

 



 

Hi-Fi優質I.T網”站內所有文章、圖片除特別加註外均屬本站所有,如有任何問題請來信告知
請支持言論自由,討論區所發表之言論不代表本站立場
Hi-Fi優質I.T網”版權所有2004©
---最佳顯示效果1024*768---