日本語利用の為の設定

広告
facebookボタン
googleplusボタン
twitterボタン
ダミーボタン
bloggerボタン

日本語などマルチバイト文字を使うには拡張モジュールの「php_mbstring.dll」を有効にします。"php.ini"の618行目付近を見てください。

現時点のphp.iniファイルでは次のように記述されています。

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename.extension
;
; For example, on Windows:
;
;   extension=msql.dll

(途中略)

;extension=php_ldap.dll
;extension=php_mbstring.dll
;extension=php_exif.dll      ; Must be after mbstring as it depends on it

拡張モジュールを有効にするには先頭の「;」を外します。よって今回は次のように変更します。

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename.extension
;
; For example, on Windows:
;
;   extension=msql.dll

(途中略)

;extension=php_ldap.dll
extension=php_mbstring.dll
;extension=php_exif.dll      ; Must be after mbstring as it depends on it
mbstringの設定

続いて「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 = EUC-JP

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

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

; 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 = auto

; 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=

多くの設定項目があります。各設定の「;」を外した後でそれぞれ設定を行っていきます。

「mbstring.internal_encoding」はマルチバイト文字列関数(mbstring)のデフォルトエンコードとなります。変換元の文字コードが指定されなかった場合、ここで指定した文字コードが使用されます。基本的には文字コードを指定しますので設定は不要ですが、ここでは基本の文字コードとして使用予定の"UTF-8"を指定しておきます。

[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

「mbstring.http_input」と「mbstring.http_output」はHTTP通信の時のインプットとアウトプットの文字コード変換に関するパラメータです。自動で変換されるとトラブルの元のようなので変換しないように"pass"を指定しておきます。

; 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

「mbstring.detect_order」は文字コードの自動判別を行う時にどの文字コードから順に確認していくのかを指定します。"auto"に設定するとどの文字コードから判別するか分かりませんので明示的に指定しておきます。

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

「mbstring.substitute_character」は変換に失敗した場合に代わりに表示する文字です。文字を出力しない場合は"none"を指定します。

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

「mbstring.strict_detection」は文字コードの自動判別を厳密に行うかどうかの設定です。それぞれメリットデメリットがありますが今回はOffのままです。

; enable strict encoding detection.
mbstring.strict_detection = Off

「mbstring.http_output_conv_mimetype」と「mbstring.script_encoding」についてはよく分かっていないので、今回はコメントのままとしておきます。

; 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=

最終的には次のように記述しました。

[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=

"php.ini"ファイルを保存すれば完了です。設定変更後にApacheを再起動して変更した内容を反映させておいて下さい。

phpinfo関数が含まれるファイルをブラウザで開き設定を確認してみます。

p5-1

設定された値が反映されているかどうか確認しておいて下さい。

( Written by T.buzz.Ikura+ )

Social Button
Facebook Page