『PHP×携帯サイト デベロッパーズバイブル』を参考に;

http://www.amazon.co.jp/gp/product/4797348461/

SakuraのStandardで掲示板を制作中です

▼ソース
a.) in_enc.php:入力文字コードを内部文字コード(SJIS-win)に変換
b.) out_enc.php:内部文字コードを出力文字コード(SJIS-win)に変換
c.) bbs.php:掲示板本体
d.) bbs_d.txt:掲示板のデータ

▼解説
c から、a,b をrequire_once し、a,bに定義したfunction(input_encode, output_encode)をc のソースの上部(a)と下部(b)で実行

※各ソースは、eucで保存

※php.iniに、以下を記入
mbstring.language = Japanese
mbstring.internal_encoding = eucJP-win
mbstring.http_input = pass
mbstring.http_output = pass
mbstring.encoding_translation = Off
mbstring.detect_order = SJIS-win, eucJP-win, JIS, UTF-8, ASCII

ブラウザで見ると、euc(ソースの文字コード)のままで、
SJIS-winに変換されてません。

何卒、ご指導くださいませ!

回答の条件
  • 1人3回まで
  • 登録:
  • 終了:2009/09/25 02:48:48
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

ベストアンサー

id:makeworld No.2

回答回数75ベストアンサー獲得回数23

ポイント50pt

php.iniに下記の設定を記入すればSJIS-winで表示されると思います。

output_buffering = On
output_handler = mb_output_handler
id:gets_itai

解決しました。

ありがとうございました。

php.iniのこと、きちんと勉強します!

2009/09/25 02:38:19

その他の回答2件)

id:tezcello No.1

回答回数460ベストアンサー獲得回数69

ポイント10pt

> SJIS-winに変換されてません。

これは

> output_encode();

が、HTMLを出力した後に実行されているからでしょう。


出力エンコードを変更するのが mbstring.http_output なら、入力も mbstring.http_input ( mbstring.encoding_translation の変更も要)にお任せしては?

id:gets_itai

output_encode(); を、html記述部より上にしましたが、それでもダメでした。

ちなみに;

ブラウザの文字コード設定を、SJISにするとphpに記述したHTML部分と入力した文字部分ともに文字化けします。でも、ブラウザの文字コードをeucにすると、phpに記述したHTML部分は文字化けしますが、入力した文字部分は文字化けせずに表示されます。

2009/09/24 10:19:03
id:makeworld No.2

回答回数75ベストアンサー獲得回数23ここでベストアンサー

ポイント50pt

php.iniに下記の設定を記入すればSJIS-winで表示されると思います。

output_buffering = On
output_handler = mb_output_handler
id:gets_itai

解決しました。

ありがとうございました。

php.iniのこと、きちんと勉強します!

2009/09/25 02:38:19
id:wate_wate No.3

回答回数45ベストアンサー獲得回数3

ポイント50pt

以下のサイトをご確認されるとお分かりになると思いますが、

ini_set('mbstring.http_output', 'SJIS-win');

を指定しても、

コンテンツの出力時の文字コード自体が変換されるわけではありません。

http://memo.xight.org/2007-02-14-1

http://hain.jp/index.php/tech-j/2007/02/13/%EF%BC%B0%EF%BC%A8%EF...


で、

私の場合は、出力時のコンテンツの文字コードを変更する場合は、

自前で変更しています。

コメント欄のソースコード例にとれば、

以下のような感じで変換しています。

<?php

require_once 'input_encode.php';

require_once 'output_encode.php';

input_encode();

if (isset($_GET['comment'])) {

$comment = $_GET['comment'];

$comment = str_replace(array("\r", "\n"), "", $comment);

}

$filename = 'mobile_bbs_data.txt';

if (!file_exists($filename)) {

touch($filename);

}

$handle = fopen($filename, 'a+');

if (isset($comment)) {

fseek($handle, 0);

fwrite($handle, $comment . "\n");

}

$comment_list = array();

if (filesize($filename) > 0) {

fseek($handle, 0);

$data = fread($handle, filesize($filename));

$comment_list = explode("\n", $data);

$comment_list = array_reverse($comment_list);

}

fclose($handle);

//追記(ここから)--------------->

ob_start();

//追記(ここまで)<---------------

?>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset='Shift_JIS'" />

<meta http-equiv="Pragma" content="no-cache" />

<meta http-equiv="Cache-Control" content="no-cache" />

<meta http-equiv="Expires" content="-1" />

<title>モバイル掲示板</title>

</head>

<body>

モバイル掲示板(Chap2)

<form action="" method="GET">

<input type="text" name="comment" value="">

<input type="submit" value="書き込み">

</form>


foreach ($comment_list as $comment_item) {

if (!empty($comment_item)) {

echo $comment_item . '


';

}

}

?>

</body>

</html>

<?php

//変更(ここから)--------------->

header("Content-type: text/html; charset=shift_jis");

$contents = ob_get_clean();

echo mb_convert_encoding("SJIS","EUC-JP",$contents);

//変更(ここまで)<---------------

?>

id:gets_itai

ありがとうございます!

PHPは、始めたばかりでまだ理解できませんが、後々非常に助けていただけるコメントだと感じます。

http://okwave.jp/qa2253365.html

なども参考になりそうですね。

2009/09/25 02:45:36
  • id:gets_itai
    ▼a.) in_enc.php:入力文字コードを内部文字コード(SJIS-win)に変換
    <?php

    /**
    *入力文字コードを内部文字コードに変換する
    */
    function input_encode()
    {
    //入力文字コードを内部文字コードに変換する
    if (isset($_GET)) {
    mb_convert_variables(mb_internal_encoding(), 'SJIS-win', $_GET);
    }
    if (isset($_POST)) {
    mb_convert_variables(mb_internal_encoding(), 'SJIS-win', $_POST);
    }
    }
    ?>



    ▼c.) bbs.php:掲示板本体

    <?php

    require_once 'input_encode.php';
    require_once 'output_encode.php';

    input_encode();

    if (isset($_GET['comment'])) {
    $comment = $_GET['comment'];
    $comment = str_replace(array("\r", "\n"), "", $comment);
    }

    $filename = 'mobile_bbs_data.txt';
    if (!file_exists($filename)) {
    touch($filename);
    }

    $handle = fopen($filename, 'a+');

    if (isset($comment)) {
    fseek($handle, 0);
    fwrite($handle, $comment . "\n");
    }
    $comment_list = array();
    if (filesize($filename) > 0) {
    fseek($handle, 0);
    $data = fread($handle, filesize($filename));
    $comment_list = explode("\n", $data);
    $comment_list = array_reverse($comment_list);
    }

    fclose($handle);

    ?>

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset='Shift_JIS'" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Cache-Control" content="no-cache" />
    <meta http-equiv="Expires" content="-1" />
    <title>モバイル掲示板</title>
    </head>
    <body>
    モバイル掲示板(Chap2)<br>
    <form action="" method="GET">
    <input type="text" name="comment" value="">
    <input type="submit" value="書き込み">
    </form>
    <hr>

    <?php
    foreach ($comment_list as $comment_item) {
    if (!empty($comment_item)) {
    echo $comment_item . '<br><hr>';
    }
    }
    ?>

    </body>
    </html>

    <?php output_encode(); ?>
  • id:gets_itai
    実際には「in_enc.php」は「input_encode.php」です。なので、綴り違いではありません。
  • id:wate_wate
    output_encode()
    の内容がわからないと答えようがないのですが・・・・
  • id:gets_itai
    失礼しました...

    ▼output_encode.php

    <?php

    function output_encode()
    {
    ini_set('mbstring.http_output', 'SJIS-win');
    }

    ?>
  • id:gets_itai
    http://muneto.murakami.biz/web/携帯サイト制作さくらで空メール登録.php

    ↑Sakura Internet の特性なのか。フルパスで require_once してみましたが、まだだめです orz...

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません