| 提供裝潢工程相關案件,線上超過上百筆精選裝潢案件 都能在這找到屬於自己的合適案件,找裝潢工程機會 | 想要搬家,讓專業的來,細心運送大小家具 免費估價專業包裝,輕鬆遷移免煩惱 |
[教學]big5 轉 utf-8 之方法 |
| 房東:小胖 發表時間:2010-06-01 | [檢舉] |
|
最近因為要寫一個應用程式,他必須把網頁傳送過來的資料轉為 utf-8 再處理, 一般 big5 轉 utf-8 用 iconv 函數就可以處理,但有一種情形,就是如果 big5 字串中如果有 就無法轉換 所以我特別寫了一個轉換程式 function big5_2_utf8($big5_str) { $sw[\"廖\"]=\"廖\"; $sw[\"�牾"]=\"�牾"; $sw[\"�茻"]=\"�茻"; $sw[\"�傜"]=\"�傜"; $sw[\"�揊"]=\"�揊"; $sw[\"�焮"]=\"�焮"; $sw[\"�莍"]=\"�莍"; $sw[\"�鄃"]=\"�鄃"; $sw[\"�幋"]=\"�幋"; $sw[\"�滜"]=\"�滜"; $sw[\"�綅"]=\"�綅"; $sw[\"�赨"]=\"�赨"; $sw[\"�塿"]=\"�塿"; $sw[\"�槙"]=\"�槙"; $sw[\"�箤"]=\"�箤"; $sw[\"�踊"]=\"�踊"; $sw[\"�嫹"]=\"�嫹"; $sw[\"�潿"]=\"�潿"; $sw[\"�蔌"]=\"�蔌"; $sw[\"�醆"]=\"�醆"; $sw[\"�嬞"]=\"�嬞"; $sw[\"�獦"]=\"�獦"; $sw[\"�螏"]=\"�螏"; $sw[\"�餤"]=\"�餤"; $sw[\"�燡"]=\"�燡"; $sw[\"�螰"]=\"�螰"; $sw[\"�駹"]=\"�駹"; $sw[\"�礒"]=\"�礒"; $sw[\"�鎪"]=\"�鎪"; $sw[\"�瀙"]=\"�瀙"; $sw[\"�酀"]=\"�酀"; $sw[\"�瀵"]=\"�瀵"; $sw[\"�騱"]=\"�騱"; $sw[\"�酅"]=\"�酅"; $sw[\"�贕"]=\"�贕"; $sw[\"�鱋"]=\"�鱋"; $sw[\"�鱭"]=\"�鱭"; $sw[\"�幋"]=\"�幋"; $sw[\"�綅"]=\"�綅"; $sw[\"�箤"]=\"�箤"; $sw[\"�嫹"]=\"�嫹"; $sw[\"█\"]=\"█\"; $sw[\"�塿"]=\"�塿"; $sw[\"/\"]=\"/\"; $len=strlen($big5_str); $un=\'\'; for ($i=0;$i<$len;$i++) { $s =substr($big5_str,$i,1); $s1 =substr($big5_str,$i,2); $sbit=ord($s); if ($sbit < 0x80) { $un.=$s; } else { $new_word=iconv(\"BIG5\",\"UTF-8\",$s1); if(!$new_word){ if($sw[$s1]) $new_word=$sw[$s1]; } if($new_word!=\'\'){ $un.=$new_word; $i++; } } } $s=\'\'; while(strlen($un)>0){ $p=strpos($un,\"\"); if ($p===false) { $s.=$un; return $s; } else{ if ($p!=0){ $s.=substr($un,0,$p); $un=substr($un,$p); } $p=strpos($un,\";\"); if ($p===false){ $s.=$un; return $s; } else{ $code=substr($un,2,$p-2); $un=substr($un,$p+1); if (strcasecmp($code{0},\"x\")==0){ $code=hexdec(substr($code,1)); } else{ $code=intval($code); } if($code <= 0x7f){ $s.=chr($code); } elseif($code <= 0x7ff){ $s.=chr(($code >> 6) | 0xc0); $s.=chr(($code % 0x40) | 0x80); } elseif($code <= 0xffff){ $s.=chr( ($code >> 12) | 0xe0); $s.=chr((($code >> 6) % 0x40) | 0x80); $s.=chr( ($code % 0x40) | 0x80); } elseif($code <= 0x10ffff){ $s.=chr( ($code >> 18) | 0xf0); $s.=chr((($code >> 12) % 0x40) | 0x80); $s.=chr((($code >> 6) % 0x40) | 0x80); $s.=chr( ($code % 0x40) | 0x80); } } } } return $s; } |
廣利不動產-新板特區指名度最高、值得您信賴的好房仲 您的托付,廣利用心為您服務 廣利不動產-板橋在地生根最實在--新板特區指名度最高、值得您信賴的好房仲 完整房訊,房屋、店面熱門精選物件,廣利不動產 優質仲介,房屋租賃、買賣資訊透明,交易真安心! |
| 1 樓住戶:小貓 發表時間:2010-06-01 | [檢舉] | |
|
因為目前論壇的編碼是 Big-5 ,為了跟綠界的便利超商付費機制結合,而不巧,他的編碼是 UTF-8,所以我傳過去的參考全變成亂碼,所以我不得已只好寫一個轉碼頁面,有需求的可以參考使用.. $pay_sob=iconv('BIG5','UTF-8',$pay_sob); $pay_name=iconv('BIG5','UTF-8',$pay_name); ?> 待會將會引導您至便利超商付費畫面,請耐心等侯... |
| 2 樓住戶:阿寶 發表時間:2010-06-01 | [檢舉] | |
|
最近寫程式會用到 utf8 轉 big5,本來想說用iconv就好了 不過我在使用iconv,將utf8轉big5時就出現一個問題,就是只要遇到utf8轉big5,沒有對應的big5碼的時候就有狀況了,原本以為就那個字沒出現,結果是當對應不到big5碼後它是以後的通通不見了,當然這下子就不妙了。 最後我只好一個字一個字轉,然後遇到沒有對應的碼就用"■"代替,雖然損失了效能,但是卻確保了正確性。 本文參考自 http://203.68.102.46/online_book/content.php?chapter_sn=173 感謝 JENY's 複製程式 //逐字轉換utf8字串為big5 function utf8_2_big5($utf8_str) { $i=0; $len = strlen($utf8_str); $big5_str=""; for ($i=0;$i<$len;$i++) { $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; } 因為個人需求,當 utf-8 轉 big5 時,對應不到的字,我想要改用 $len = strlen($utf8_str); $big5_str=""; for ($i=0;$i<$len;$i++) { $s =substr($utf8_str,$i,1); $s1=substr($utf8_str,$i+1,1); $s2=substr($utf8_str,$i+2,1); $s3=substr($utf8_str,$i+3,1); $sbit=ord($s); if ($sbit < 0x80) { $big5_str.=$s; } else if($sbit >= 0xc0 && $sbit < 0xe0) { $new_word=iconv("UTF-8","Big5",substr($utf8_str,$i,2)); $big5_str.=$new_word?$new_word: ''.(((ord($s) & 0x1f) * 0x40) + (ord($s1) & 0x3f)).'' $i++; } else if($sbit >= 0xe0 && $sbit < 0xf0) { $new_word=iconv("UTF-8","Big5",substr($utf8_str,$i,3)); $big5_str.=$new_word?$new_word: ''.(((ord($s) & 0x0f) * 0x1000) + ((ord($s1) & 0x3f) * 0x40) + (ord($s2) & 0x3f)).'' $i+=2; } else if($sbit >= 0xf0 && $sbit < 0xf8) { $new_word=iconv("UTF-8","Big5",substr($utf8_str,$i,4)); $big5_str.=$new_word?$new_word: ''.(((ord($s) & 0x07) * 0x40000) + ((ord($s1) & 0x3f) * 0x1000) + ((ord($s2) & 0x3f) * 0x40) + (ord($s3) & 0x3f)).'' $i+=3; } } return $big5_str; } [/code] [/sell] |