source: branches/feature-module-update/convert.php @ 15603

Revision 15603, 3.0 KB checked in by nanasess, 17 years ago (diff)

エンコーディングを UTF-8 に変更

  • Property svn:executable set to *
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1#!/usr/local/bin/php
2<?php
3/*
4 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
5 *
6 * http://www.lockon.co.jp/
7 */
8
9/**
10 * ファイルのエンコーディングを $fromEncoding から $toEncoding へ変換します.
11 *
12 * @author  Kentaro Ohkouchi<ohkouchi@loop-az.jp>
13 * @since   PHP4.3.0(cli)
14 * @version $Id:convert.php 15079 2007-07-20 07:20:36Z nanasess $
15 */
16
17/**
18 * 変換したいファイルの拡張子をカンマ区切りで羅列.
19 */
20$includes = "php,inc,tpl,css,sql,js";
21
22/**
23 * 除外するファイル名をカンマ区切りで羅列.
24 */
25$excludes = "convert.php";
26
27/**
28 * 変換元エンコーディング.
29 */
30$fromEncoding = "EUC-JP";
31
32/**
33 * 変換先エンコーディング.
34 */
35$toEncoding = "UTF-8";
36
37$includeArray = explode(',', $includes);
38$excludeArray = explode(',', $excludes);
39$fileArrays = listdirs('.');
40
41foreach ($fileArrays as $path) {
42    if (is_file($path)) {
43
44        // ファイル名を取得
45        $fileName = pathinfo($path, PATHINFO_BASENAME);
46
47        // 拡張子を取得
48        $suffix = pathinfo($path, PATHINFO_EXTENSION);
49
50        // 除外ファイルをスキップ
51        if (in_array($fileName, $excludeArray)) {
52            echo "excludes by " . $path . "\n";
53            continue;
54        }
55
56        // 変換対象を順に処理
57        foreach ($includeArray as $include) {
58            if ($suffix == $include) {
59
60                // ファイル内容を取得し, エンコーディング変換
61                $contents = file_get_contents($path);
62                $convertedContents = mb_convert_encoding($contents,
63                                                         $toEncoding,
64                                                         $fromEncoding);
65
66                // 書き込みできるか?
67                if (is_writable($path)) {
68
69                    // ファイルを書き出しモードで開く
70                    $handle = fopen($path, "w");
71                    if (!$handle) {
72                        echo "Cannot open file (". $path . ")";
73                        continue;
74                    }
75
76                    // コード変換した内容を書き込む
77                    if (fwrite($handle, $convertedContents) === false) {
78                        echo "Cannot write to file (" . $path . ")";
79                        continue;
80                    }
81
82                    echo "converted " . $path . "\n";
83                    // ファイルを閉じる
84                    fclose($handle);
85                } else {
86
87                    echo "The file " . $filename . "is not writable";
88                }
89            }
90        }
91    }
92}
93
94/**
95 * $dir を再帰的に辿ってパス名を配列で返す.
96 *
97 * @param string 任意のパス名
98 * @return array $dir より下層に存在するパス名の配列
99 * @see http://www.php.net/glob
100 */
101function listdirs($dir) {
102    static $alldirs = array();
103    $dirs = glob($dir . '/*');
104    if (count($dirs) > 0) {
105        foreach ($dirs as $d) $alldirs[] = $d;
106    }
107    foreach ($dirs as $dir) listdirs($dir);
108    return $alldirs;
109}
110?>
Note: See TracBrowser for help on using the repository browser.