2011/03/23

PHP on-line Big5->UTF8 convert function

若資料庫是 Big5, web page 卻已經寫成 UTF8 code...

如果只有一兩欄有中文字 可以在SQL command中直接這樣用:
CONVERT(fields USING big5)

如果資料欄位多 可以用這小 function 去做線上 Big5->UTF8 轉換
mysql_query("SET NAMEs 'big5'"); big52utf8($row['fields']) function big52utf8($big5str) { $blen = strlen($big5str); $utf8str = ""; for($i=0; $i<$blen; $i++) { $sbit = ord(substr($big5str, $i, 1)); if ($sbit < 129) { $utf8str.=substr($big5str,$i,1); }elseif ($sbit > 128 && $sbit < 255) { $new_word = iconv("BIG5", "UTF-8", substr($big5str,$i,2)); $utf8str.=($new_word=="")?"?":$new_word; $i++; } } return $utf8str; }// end func // 參考 http://twpug.net/modules/newbb/viewtopic.php?post_id=11762

沒有留言:

張貼留言