引用 | 編輯
aa
2008-08-29 15:40 |
樓主
|
||
x0
在轉換 MySQL 資料庫,由 big5 轉為 utf-8 後,發現資料庫中有很多 的字元,這些都是之前被 IE 轉進去的 utf-8 文字,因為考量到搜尋的問題,所以要把這些字元轉換為真正的 utf-8 才可以因此我特別寫了一個小程式來轉 複製程式 function utf8($un) { $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{ $code1=substr($un,0,$p); if($p>10) $s.=$code1; 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.=$code1; } 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; } 只要將有 的字串丟進去,就可以轉換為真正的 utf-8,不過因為文章中的單引號及雙引號,之前已被轉為 ,這些不用再轉回,所以我加了判斷,針對小於 0x7f 的字元不做轉換 x0
|