1. 用 vi /etc/httpd/conf/httpd.conf 設定Apache中的語系為:( (記得restart)
AddDefaultCharset UTF-8
2. 用 vi /etc/php.ini 設定php中的語系為:( (記得restart)
default_charset = "utf-8"
3. 用 vi /etc/my.cnf 設定MySQL中的語系為:( (記得restart)
[mysqld]
init_connect='SET NAMES utf8'
default-character-set=utf8
[client]
default-character-set = utf8
4. 建立資料庫時選擇語系: (記得清除DB Cache)
DROP DATABASE IF EXISTS `aa`;
CREATE DATABASE `aa` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
USE `aa`;
CREATE TABLE IF NOT EXISTS `aat` (
`id` char(1) NOT NULL default '1',
`myStr` varchar(200) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
5. 用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
6. 在php檔中必須加入:
<meta equiv=``Content-Type`` content=``text/html; charset=UTF-8``>
7. 在連接DB的檔中必須加入3行mysql_query才ok:
$host="localhost"; $DBname="aa";
$user= "root"; $passwd = "";
$link = mysql_connect($host,$user,$passwd) or die ("Fail");
$db = mysql_select_db($DBname, $link) or die ("Fail");
// 要在真正query DB取出資料前,加入以下3行
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER_SET_CLIENT=utf8");
mysql_query("SET CHARACTER_SET_RESULTS=utf8");
$sql = "select * from aat where crid='1'";
$rows = mysql_query($sql);
8. 在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
參考出處:請點我
沒有留言:
張貼留言