網路城邦

上一篇 回創作列表 下一篇   字體:
php big5跟utf-8互轉的指令
2014/05/20 19:37:51瀏覽3960|回應0|推薦0

php big5跟utf-8互轉的指令

big5 轉 utf-8
PHP 程式碼:
$str=iconv("big5","UTF-8",$str);

utf-8 轉 big5
PHP 程式碼:
$str=iconv("UTF-8","big5",$str);


摘自 http://blog.bestdaylong.com/2007/06/php-big5utf-8.html

// convert UTF-8 string to NCR mb_convert_encoding($utf8_str, 'HTML-ENTITIES', 'utf-8');

摘自 http://tzangms.com/ncr_e8bd89e68f9b/


http://yoonow.pixnet.net/blog/post/11141571

 
我想大家都知道utf8轉big5,那就用iconv嗎!
不過我在使用iconv,將utf8轉big5時就出現一個問題,就是只要遇到utf8轉big5,沒有對應的big5碼的時候就有狀況了,原本以為就那個字沒出現,結果勒,是當對應不到big5碼後它是以後的通通不見了,當然這下子就不妙了。

最後我只好一個字一個字轉,然後遇到沒有對應的碼就用"■"代替,雖然損失了效能,但是卻確保了正確性。

程式碼如下:

          //逐字轉換utf8字串為big5
        function  utf8_2_big5($utf8_str)  {
                $i=0;
                $len  =  strlen($utf8_str);
                $big5_str="";
                for  ($i=0;$i<$len;$i++) br="">                        $sbit  =  ord(substr($utf8_str,$i,1));
                        if  ($sbit  <  128)  {
                                $big5_str.=substr($utf8_str,$i,1);
                        }  else  if($sbit  >  191  &&  $sbit  <  224)  {
                                $new_word=iconv("UTF-8","Big5",substr($utf8_str,$i,2));
                                $big5_str.=($new_word=="")?"■":$new_word;
                                $i++;
                        }  else  if($sbit  >  223  &&  $sbit  <  240)  {
                                $new_word=iconv("UTF-8","Big5",substr($utf8_str,$i,3));
                                $big5_str.=($new_word=="")?"■":$new_word;
                                $i+=2;
                        }  else  if($sbit  >  239  &&  $sbit  <  248)  {
                                $new_word=iconv("UTF-8","Big5",substr($utf8_str,$i,4));
                                $big5_str.=($new_word=="")?"■":$new_word;
                                $i+=3;
                        }
                }
                return  $big5_str;
    }  



小的修訂版 將超過big5字轉NCR

  $str= utf8_2_big5($str);


function  utf8_2_ncr($utf8_str){
  $ncr=mb_convert_encoding($utf8_str, 'HTML-ENTITIES', 'utf-8');
  return $ncr;
}

function  utf8_2_big5($utf8_str)  {
                $i=0;
                $len  =  strlen($utf8_str);
                $big5_str="";
                for  ($i=0;$i<$len;$i++) br="">                        $sbit  =  ord(substr($utf8_str,$i,1));
                        if  ($sbit  <  128)  {
                                $big5_str.=substr($utf8_str,$i,1);
                        }  else  if($sbit  >  191  &&  $sbit  <  224)  {
                                $new_word=iconv("UTF-8","Big5",substr($utf8_str,$i,2));
                                $big5_str.=($new_word=="")?utf8_2_ncr(substr($utf8_str,$i,2)):$new_word;
                                $i++;
                        }  else  if($sbit  >  223  &&  $sbit  <  240)  {
                                $new_word=iconv("UTF-8","Big5",substr($utf8_str,$i,3));
                                $big5_str.=($new_word=="")?utf8_2_ncr(substr($utf8_str,$i,3)):$new_word;
                                $i+=2;
                        }  else  if($sbit  >  239  &&  $sbit  <  248)  {
                                $new_word=iconv("UTF-8","Big5",substr($utf8_str,$i,4));
                                $big5_str.=($new_word=="")?utf8_2_ncr(substr($utf8_str,$i,4)):$new_word;
                                $i+=3;
                        }
                }
                return  $big5_str;
    }


( 知識學習語言 )
回應 推薦文章 列印 加入我的文摘
上一篇 回創作列表 下一篇

引用
引用網址:https://classic-blog.udn.com/article/trackback.jsp?uid=ae&aid=13488181