ユーザ用ツール

PHPと文字コード

PHPを利用しているときに、文字コードが思ったように扱われないという問題が起こることがある。mbstrings関連の処理において発生するこの種の問題は、設定で回避することが出来る。

回避方法

php.iniまたは、httpd.confあるいは.htaccessに次の設定をすればいいだろう。 (httpd.confや.htaccessは当然、httpd経由でPHPを利用する場合のみ有効。)

default_charset = "EUC-JP"
mbstring.language = Japanese
mbstring.internal_encoding = EUC-JP
mbstring.encoding_translation = On
mbstring.http_input = auto
mbstring.http_output = pass
<IfModule mod_php4.c>
 php_value default_charset EUC-JP
 php_value mbstring.language Japanese
 php_value mbstring.internal_encoding EUC-JP
 php_flag  mbstring.encoding_translation On
 php_value mbstring.http_input auto
 php_value mbstring.http_output pass
</IfModule>

これは、内部でEUCを使っている場合である。UTF-8を使っているなら、上のEUC-JPをUTF-8に置き換えればいい。この設定がないと、例えばNucleusのxml-rss2.phpが生成するRSSの文字コードが正しくUTF-8にならずに、妙な壊れたページを吐き出すなどの障害となることがある。

PHPの生成するページの文字コードがおかしい場合には、このあたりの設定を見直してみるとよい。

Tipsへ戻る。

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also, you acknowledge that you have read and understand our Privacy Policy. If you do not agree, please leave the website.

More information