- Home ›
- PHPインストールと初期設定 ›
- HERE
日本語利用の為の設定
日本語などマルチバイト文字を使うには拡張モジュールの「php_mbstring.dll」を有効にし、その後でmbstringに関する設定を行う必要があります。ここではPHPで日本語を利用する場合の設定方法について解説します。
1.php_mbstring.dllを有効にする
2.mbstringに関する設定
「php.ini」をテキストエディタで開き852行目付近を見てください。次のように記述されています。
;;;;;;;;;;;;;;;;;;;;;; ; Dynamic Extensions ; ;;;;;;;;;;;;;;;;;;;;;; ; If you wish to have an extension loaded automatically, use the following ; syntax: ; ; extension=modulename.extension ; ; For example, on Windows: ; ... ... ;extension=php_mbstring.dll ... ...
拡張モジュールを有効にするには先頭の「;」を外します。よって今回は次のように変更します。
;;;;;;;;;;;;;;;;;;;;;;
; 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_mbstring.dll
...
...
以上でphp_mbstring.dllの有効化は完了です。
続いて「mbstring」の設定を行います。「php.ini」ファイルでは次のように記述されています。
[mbstring] ; language for internal character representation. ; This affects mb_send_mail() and mbstrig.detect_order. ; http://php.net/mbstring.language ;mbstring.language = Japanese ; Use of this INI entry is deprecated, use global internal_encoding instead. ; internal/script encoding. ; Some encoding cannot work as internal encoding. (e.g. SJIS, BIG5, ISO-2022-*) ; If empty, default_charset or internal_encoding or iconv.internal_encoding is used. ; The precedence is: default_charset < internal_encoding < iconv.internal_encoding ;mbstring.internal_encoding = ; Use of this INI entry is deprecated, use global input_encoding instead. ; http input encoding. ; mbstring.encoding_traslation = On is needed to use this setting. ; If empty, default_charset or input_encoding or mbstring.input is used. ; The precedence is: default_charset < intput_encoding < mbsting.http_input ; http://php.net/mbstring.http-input ;mbstring.http_input = ; Use of this INI entry is deprecated, use global output_encoding instead. ; http output encoding. ; mb_output_handler must be registered as output buffer to function. ; If empty, default_charset or output_encoding or mbstring.http_output is used. ; The precedence is: default_charset < output_encoding < mbstring.http_output ; To use an output encoding conversion, mbstring's output handler must be set ; otherwise output encoding conversion cannot be performed. ; http://php.net/mbstring.http-output ;mbstring.http_output = ; 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" detect order is changed according to mbstring.language ; 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. ; Default: Off ;mbstring.strict_detection = On ; 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=
多くの設定項目があります。各設定の「;」を外した後でそれぞれ設定を行っていきます。
「mbstring.internal_encoding」はマルチバイト文字列関数(mbstring)のデフォルトエンコードとなります。変換元の文字コードが指定されなかった場合、ここで指定した文字コードが使用されます。基本的には文字コードを指定しますので設定は不要ですが、ここでは基本の文字コードとして使用予定の"UTF-8"を指定しておきます。
; language for internal character representation. ; This affects mb_send_mail() and mbstrig.detect_order. ; http://php.net/mbstring.language mbstring.language = Japanese ; Use of this INI entry is deprecated, use global internal_encoding instead. ; internal/script encoding. ; Some encoding cannot work as internal encoding. (e.g. SJIS, BIG5, ISO-2022-*) ; If empty, default_charset or internal_encoding or iconv.internal_encoding is used. ; The precedence is: default_charset < internal_encoding < iconv.internal_encoding mbstring.internal_encoding = UTF-8
「mbstring.http_input」と「mbstring.http_output」はHTTP通信の時のインプットとアウトプットの文字コード変換に関するパラメータです。自動で変換されるとトラブルの元のようなので変換しないように"pass"を指定しておきます。
; Use of this INI entry is deprecated, use global input_encoding instead. ; http input encoding. ; mbstring.encoding_traslation = On is needed to use this setting. ; If empty, default_charset or input_encoding or mbstring.input is used. ; The precedence is: default_charset < intput_encoding < mbsting.http_input ; http://php.net/mbstring.http-input mbstring.http_input = pass ; Use of this INI entry is deprecated, use global output_encoding instead. ; http output encoding. ; mb_output_handler must be registered as output buffer to function. ; If empty, default_charset or output_encoding or mbstring.http_output is used. ; The precedence is: default_charset < output_encoding < mbstring.http_output ; To use an output encoding conversion, mbstring's output handler must be set ; otherwise output encoding conversion cannot be performed. ; 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" detect order is changed according to mbstring.language
; 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.func_overload」はシングルバイト対応の関数をマルチバイト対応の関数でオーバーロードするどうかの設定です。自動でオーバーロードされると予期せぬ不具合が発生することもありますのでオーバーロードしない「0」としておきます。
; 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
「mbstring.strict_detection」は文字コードの自動判別を厳密に行うかどうかの設定です。それぞれメリットデメリットがありますが今回はOffのままです。
; enable strict encoding detection.
; Default: Off
mbstring.strict_detection = Off
「mbstring.http_output_conv_mimetype」についてはよく分かっていないので今回はコメントのままとしておきます。
; 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=
最終的には次のように記述しました。
[mbstring] mbstring.language = Japanese mbstring.internal_encoding = UTF-8 mbstring.http_input = pass mbstring.http_output = pass mbstring.encoding_translation = Off mbstring.detect_order = UTF-8,SJIS,EUC-JP,JIS,ASCII mbstring.substitute_character = none mbstring.func_overload = 0 mbstring.strict_detection = Off ;mbstring.http_output_conv_mimetype=
「php.ini」ファイルを保存すれば完了です。設定変更後にApacheを再起動して変更した内容を反映させておいて下さい。phpinfo関数が含まれるファイルをブラウザで開き設定を確認してみます。
設定された値が反映されているかどうか確認しておいて下さい。
( Written by Tatsuo Ikura )