プロ・クリエータの実践ウェブテクニック集



日本語文字セットの設定

 .htaccess ファイルで、PHP の "mbstring"(日本語などのマルチバイトストリング)関数を設定します。

 JIS, Shift_JIS, ISO-2022-JP, BIG-5 で書かれた PHPス クリプトは動作しない可能性があります。 文字エンコーディングの問題点について精通していない限り、内部エンコーディングとして使用するべきではありません。 UTF-8, もしくは EUC-JP を推奨します。

 PHP でデータベースを利用する場合、誤動作を防ぐ為にPHP と データベースの内部エンコーディングについて同じ文字エンコーディングを使用することを推奨します。

 *注  セキュリティ保護の為 ".htaccess" ファイルの設置を許可していないサーバや SuPHP(mod_suPHP モジュール)を採用して一部の機能を制限しているサーバも存在します。 移植性を考慮するなら PHP 関連の設定はできるだけ "php.ini" や php スクリプト内で記述する事を推奨します。
参照: 日本語文字セットの設定 mbstring

名前 説明 初期値 対応
IfModule 使用する php モジュールの振り分け。
   
php_value PHP の値を設定。    
php_flag PHP のスイッチを設定。    
default_charset デフォルトの文字セット ""  
output_buffering 出力バッファリングの有効・無効 "0"  
output_handler 出力変換の有効・無効 NULL php >= 4.0.4
mbstring.language デフォルトの言語 neutral (utf-8) php >= 4.3.0
mbstring.encoding_translation 内部文字エンコーディングの有効・無効 "0" php >= 4.3.0
mbstring.http_input HTTP 入力文字エンコーディング "pass" php >= 4.0.6
mbstring.http_output HTTP 出力文字エンコーディング "pass" php >= 4.0.6
mbstring.internal_encoding 内部文字エンコーディング
*注 mbstring.language の下位に書く必要があります。
NULL php >= 4.0.6
mbstring.substitute_character 無効な文字の代替出力の有効・無効 NULL php >= 4.0.6

UTF-8 で作成・保存して UTF-8 で表示させる場合

 *注 php5 のみを記述する場合は <IfModule> タグは不要です。

# PHP5. <IfModule mod_php5.c> php_flag output_buffering Off php_value output_handler none php_value default_charset UTF-8 php_value mbstring.language japanese php_flag mbstring.encoding_translation On php_value mbstring.http_input auto php_value mbstring.http_output UTF-8 php_value mbstring.internal_encoding UTF-8 php_value mbstring.substitute_character none </IfModule> # PHP4. <IfModule mod_php4.c> php_flag output_buffering Off php_value output_handler none php_value default_charset UTF-8 php_value mbstring.language japanese php_flag mbstring.encoding_translation On php_value mbstring.http_input auto php_value mbstring.http_output UTF-8 php_value mbstring.internal_encoding UTF-8 php_value mbstring.substitute_character none </IfModule>

UTF-8 で作成・保存して Shift_JIS で表示させる場合

# PHP5. <IfModule mod_php5.c> php_flag output_buffering On php_value output_handler mb_output_handler php_value default_charset Shift_JIS php_value mbstring.language japanese php_flag mbstring.encoding_translation On php_value mbstring.http_input auto php_value mbstring.http_output SJIS php_value mbstring.internal_encoding UTF-8 php_value mbstring.substitute_character none </IfModule> # PHP4. <IfModule mod_php4.c> php_flag output_buffering On php_value output_handler mb_output_handler php_value default_charset Shift_JIS php_value mbstring.language japanese php_flag mbstring.encoding_translation On php_value mbstring.http_input auto php_value mbstring.http_output SJIS php_value mbstring.internal_encoding UTF-8 php_value mbstring.substitute_character none </IfModule>

EUC-JP で作成・保存して EUC-JP で表示させる場合

# PHP5. <IfModule mod_php5.c> php_flag output_buffering Off php_value output_handler none php_value default_charset EUC-JP php_value mbstring.language japanese php_flag mbstring.encoding_translation On php_value mbstring.http_input auto php_value mbstring.http_output EUC-JP php_value mbstring.internal_encoding EUC-JP php_value mbstring.substitute_character none </IfModule> # PHP4. <IfModule mod_php4.c> php_flag output_buffering Off php_value output_handler none php_value default_charset EUC-JP php_value mbstring.language japanese php_flag mbstring.encoding_translation On php_value mbstring.http_input auto php_value mbstring.http_output EUC-JP php_value mbstring.internal_encoding EUC-JP php_value mbstring.substitute_character none </IfModule>

EUC-JP で作成・保存して Shift_JIS で表示させる場合

# PHP5. <IfModule mod_php5.c> php_flag output_buffering On php_value output_handler mb_output_handler php_value default_charset Shift_JIS php_value mbstring.language japanese php_flag mbstring.encoding_translation On php_value mbstring.http_input auto php_value mbstring.http_output SJIS php_value mbstring.internal_encoding EUC-JP php_value mbstring.substitute_character none </IfModule> # PHP4. <IfModule mod_php4.c> php_flag output_buffering On php_value output_handler mb_output_handler php_value default_charset Shift_JIS php_value mbstring.language japanese php_flag mbstring.encoding_translation On php_value mbstring.http_input auto php_value mbstring.http_output SJIS php_value mbstring.internal_encoding EUC-JP php_value mbstring.substitute_character none </IfModule>