CakePHP1.3.6:文字化け対応

自分の開発機は3台。

この中で一番使ってるのが、LenovoのFedora13。とっても軽くて、電池も長持ちする。
(最近、格安のx60と電池パックを買って、Ubuntu10.10を入れた。これもよさそうだが、まだ、セットアップの途中)

Windows7の2台をPHP5.2から5.3系に上げて、これまで作ってきたコードをテストしたら文字化けする。
Windows間でも違っちゃったりするので、面白い。

とりあえず、まとめておく。

PHPに関して

PHPはmbstringの設定くらい。以下がphp.iniの該当箇所。

[mbstring]
; language for internal character representation.
; http://php.net/mbstring.language
mbstring.language = Japanese

; internal/script encoding.
; Some encoding cannot work as internal encoding.
; (e.g. SJIS, BIG5, ISO-2022-*)
; http://php.net/mbstring.internal-encoding
mbstring.internal_encoding = UTF-8

; http input encoding.
; http://php.net/mbstring.http-input
mbstring.http_input = pass

; http output encoding. mb_output_handler must be
; registered as output buffer to function
; http://php.net/mbstring.http-output
mbstring.http_output = pass

; enable automatic encoding translation according to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
;       portable libs/applications.
; http://php.net/mbstring.encoding-translation
mbstring.encoding_translation = Off

; automatic encoding detection order.
; auto means
; http://php.net/mbstring.detect-order
mbstring.detect_order = UTF-8,SJIS,EUC-JP,JIS,ASCII

; substitute_character used when character cannot be converted
; one from another
; http://php.net/mbstring.substitute-character
;mbstring.substitute_character = none;

; overload(replace) single byte functions by mbstring functions.
; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
; etc. Possible values are 0,1,2,4 or combination of them.
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
; http://php.net/mbstring.func-overload
;mbstring.func_overload = 0

; enable strict encoding detection.
;mbstring.strict_detection = Off

; This directive specifies the regex pattern of content types for which mb_output_handler()
; is activated.
; Default: mbstring.http_output_conv_mimetype=^(text/|application/xhtml\+xml)
;mbstring.http_output_conv_mimetype=

; Allows to set script encoding. Only affects if PHP is compiled with --enable-zend-multibyte
; Default: ""
;mbstring.script_encoding=UTF-8

MySQLに関して

My.ini(My.cnf)はちゃんと設定しないといけません。
デフォルトだとlatin1になっちゃうので、default-character-setをutf8に設定。

[mysql]
default-character-set=utf8


# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this 
# file.
#
[mysqld]

# The TCP/IP Port the MySQL Server will listen on
port=3306

...........

# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=utf8

CakePHPの設定

Sun Limited Mt.さんのログにあるようにconf/databease.php設定する。
手持ちの機械だと、Windows7(PHP5.3.4)とFedora13(PHP5.3.3)で以下のような違いがあった。

後者は今一つ納得がいかないが、とりあえず覚書。

class DATABASE_CONFIG {

	var $default = array(
		'driver' => 'mysqli',
		'persistent' => false,
		'host' => '127.0.0.1',
		'login' => 'xxx',
		'password' => 'xxx',
		'database' => 'xxx',
		'prefix' => '',
		'encoding' => 'utf8'
	);

	var $test = array(
		'driver' => 'mysql',
		'persistent' => false,
		'host' => 'localhost',
		'login' => 'user',
		'password' => 'password',
		'database' => 'test_database_name',
		'prefix' => '',
	);
}

その他

cake.generic.cssエンコード指定。

webroot/css/cake.generic.cssの先頭に以下を追加。

* @charset "utf-8";