預設 UTF-8 編碼

1 篇文章 / 0 new
author
預設 UTF-8 編碼
Apache 設定 httpd.conf
AddDefaultCharset UTF-8

php 設定用 php.ini
default_charset = "utf-8"
程式內
PDO :
//$this->conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8",  USER, PASS,
$this->conn = new PDO("mysql:host=$host;dbname=$db",  USER, PASS,
    array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
));
ADOBE :
mysql_query("SET NAMES 'utf8'"); 每次mysql_query()前設置.
這樣方式較為麻煩, 可直接設置資料庫預設參數, 就不用每次均設定, 除非是系統需同時面對不同語系編碼.

MySQL 設定 my.cnf
[mysqld]
init_connect='SET NAMES utf8'
character-set-server=utf8
[client]
default-character-set = utf8 (不一定需要)

► 其它參考
運用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
Free Web Hosting