1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
//Model
function encSjistoEuc($str)
{
    if ($str == "") {
        return $str;
    }
    $str = mb_convert_encoding($str, "EUC-JP", "SJIS");
    return $str;
}

function createFile($file, $data)
{
    //ファイル保存
    $fp = @fopen($file, "w+");
    if (!$fp) {
        return false;
    } else {
        $ret = @fwrite($fp, $data);
        if (!$ret) {
            return false;
        }
        fclose($fp);
    }
    return true;
}

//Control
//回答1
$text = "文字コード変換に挑戦";
$new_text = encSjistoEuc($text);

//回答2
//既存ファイルの読込
$text = file_get_contents("/usr/local/sample_data_sjis.txt");
//中身のエンコード
$new_text = encSjistoEuc($text);
//EUCファイル出力
$res = createFile("/usr/local/sample_data_euc.txt", $text);
?>