PHPのテンプレートエンジンで便利なSmarty。
テンプレートをSJISで作るともちろん文字化けが発生する。
PHP(EUC) + SmartyTemplate(SJIS)で文字化けさせずに表示させるメモ。
想定環境
- php.ini
-
- mb_string.language = Japanese
- mb_string.internal_encoding = EUC-JP
- mb_string.http_input = auto
- mb_string.http_output = EUC-JP
- PHPソースは、EUCで作成
- Smarty Templateは、EUCで作成
sjis_output.php
<?php
/* Smarty Include */
require_once('Smarty/Smarty.class.php');
/* mb_string.http_output 変更 */
mb_http_output('SJIS');
$output_page = new MySmarty();
/* Smarty テンプレートをコンパイル前にSJISへ変換 */
$output_page->register_prefilter("convert_encoding_to_eucjp");
/* Smarty使用テンプレートをEUCへ統一 */
$output_page->register_postfilter("convert_encoding_to_sjis");
//表示
$output_page->display("xxxx.tmpl");
/**
* テンプレート文字コードSJIS変換
**/
function convert_encoding_to_eucjp($template_source, &$smart)
{
if (function_exists("mb_convert_encoding")) {
$enc = mb_detect_encoding($template_source, "auto");
if ($enc != "EUC-JP") {
return mb_convert_encoding($template_source, "EUC-JP", $enc);
}
return $template_source;
}
}
/**
* テンプレート文字コードEUC変換()
**/
function convert_encoding_to_sjis($template_source, &$smart)
{
if (function_exists("mb_convert_encoding")) {
return mb_convert_encoding($template_source, "SJIS", "EUC-JP");
}
return $template_source;
}
?>
Smarty マニュアル
>> register_prefilter
>> register_postfilter
>> register_outputfilter
12月 25 2006
EUCエンコードのPHP + SmartyでSJIS(Shift_JIS)出力する方法(モバイルページなど)
PHPのテンプレートエンジンで便利なSmarty。
テンプレートをSJISで作るともちろん文字化けが発生する。
PHP(EUC) + SmartyTemplate(SJIS)で文字化けさせずに表示させるメモ。
想定環境
sjis_output.php
Smarty マニュアル
>> register_prefilter
>> register_postfilter
>> register_outputfilter
By KYO • PHP, Smaty • Tags: charset, php, smarty