1. PHP 檔本身的編碼格式是 utf8
用UltraEdit(v11.20a版) 轉換所有ANSI格式的php檔案轉化為UTF-8格式: File --> Conversions --> ASCII to UTF-8 (Unicoding Editing) ( 在UltraEdit中按Advanced --> configuration --> File Handling --> Unicode/UTF-8 Detection --> 剔選Auto detect utf-8 files ) 如有需要時,可執行Remove BOM.php. 當用WinXP的Notepad將php檔由ANSI轉為UTF-8時, 因檔頭有BOM,會引起排版問題,故必須移除,執行Remove BOM.php即可自動移除. Remove BOM.php可由以下網址下載: http://www.hoyo.idv.tw/hoyoweb/document/view.php?sid=13&author=hoyo&status=view
2. MySQL 資料表的欄位的校對屬性設為 utf8_unicode_ci,就是欄位加上 collate utf8_unicode_ci
用於匯入.sql 檔案時 ,尾巴加上ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
create table tablename ( id int not null auto_increment, title varchar(20) not null, contnet varchar(300) defalut null, primary key ('id') )ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
3. PHP 在與 MySQL 溝通時,要先送出 SET NAMES utf8
mysql_query("SET NAMES 'utf8'"); mysql_query("SET CHARACTER_SET_CLIENT=utf8"); mysql_query("SET CHARACTER_SET_RESULTS=utf8");
4. PHP 在輸出畫面前,要先送出
header('Content-type: text/html; charset=utf-8');
或者
<html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head><body>
5.補充:在php檔中, 如有需要須注意: [Optional]
運用htmlentities和htmlspecialchars時,要似如下:
$chars = htmlentities($chars,ENT_QUOTES,"UTF-8");
$chars = htmlspecialchars($chars,ENT_QUOTES,"UTF-8");
並且在顯示前要用
$chars = html_entity_decode($chars,ENT_QUOTES,"UTF8");
如有用過addslashes()或mysql_real_escape_string()記得用以下:
$chars = stripslashes($chars);
如有需要可以用以下function將不同編碼轉換:
$chars = iconv('Big5','UTF-8',$chars); //由Big5轉為UTF-8