̃Gg[͂ĂȃubN}[Nɒlj

PHP :: CLI / マルチバイト変換プログラム(mbConvertEncoding.php)



標準入力(STDIN)からファイルやストリームを読み込んで、文字コード変換して標準出力(STDOUT)に出力するプログラム。
nkf コマンドは同様の機能があり、もっと高機能ですが、nkf コマンドを入れるまでもない時や入れることができない時に使えます。
もちろん CLI版のphpがインストールされていることが前提ですが。
なお、Cygwin を含め、Windows上では動作しません。

設置方法

  1. 本体スクリプト(mbConvertEncoding.php)を ~/bin/mbConvertEncoding.php に設置します。
  2. mbConvertEncoding.php の1行目の php パスを環境に合わせて書き直します。
  3. chmod 755 ~/bin/mbConvertEncoding.php として実行権限を与えます。
  4. ~/bin 配下にシンボリックリンクを作成します。(mkSymLink.sh を使えば簡単に作成できます)
    • mkSymLink.sh を設置する時は、改行コードを必ず LF にしてください。
    • chmod 755 ~/mkSymLink.sh として実行権限を与えてから実行してください。

コマンドの説明

コマンド説明
e2uEUC-JP から UTF-8 へ変換
e2sEUC-JP から SJIS へ変換
e2jEUC-JP から JIS へ変換
s2uSJIS から UTF-8 へ変換
s2jSJIS から JIS へ変換
s2eSJIS から EUC-JP へ変換
j2uJIS から UTF-8 へ変換
j2sJIS から SJIS へ変換
j2eJIS から EUC-JP へ変換
u2sUTF-8 から SJIS へ変換
u2jUTF-8 から JIS へ変換
u2eUTF-8 から EUC-JP へ変換

使い方

パイプで渡す

% less hoge.txt | e2u 

パイプで渡してソートする

% less hoge.txt | e2u | sort

ファイルを与える

% e2u < input.txt

ファイルを与えて、変換後のファイルを作成する

% e2u < input.txt > output.txt

mbConvertEncoding.php

#!/usr/local/bin/php
#
# - Description -
# convert encoding to {euc-jp, sjis, jis, utf-8} from {euc-jp, sjis, jis, utf-8}
#
# - Setting -
# 1. put this script file in /$HOME/bin
# 2. chmod 755 mbConvertEncoding.php
# 3. ln -s mbConvertEncoding.php e2u
#    ln -s mbConvertEncoding.php e2s
#    ln -s mbConvertEncoding.php e2j
#     :
#     s2u s2j s2e j2u j2s j2e u2s u2e u2j
#
# - Usage -
# some_comannds | e2u 
# some_comannds | e2u | sort
# e2u < input.txt
# e2u < input.txt > output.txt
#
<?php
 
define('EUC',  'EUC-JP');
define('SJIS', 'SJIS');
define('JIS',  'JIS');
define('UTF8', 'UTF-8');
 
$cmd = substr($argv[0], -3);
 
switch ($cmd)
{
    case 'e2u':
        convert(EUC, UTF8); break;
 
    case 'e2s':
        convert(EUC, SJIS); break;
 
    case 'e2j':
        convert(EUC, JIS); break;
 
    case 's2u':
        convert(SJIS, UTF8); break;
 
    case 's2j':
        convert(SJIS, JIS); break;
 
    case 's2e':
        convert(SJIS, EUC); break;
 
    case 'j2u':
        convert(JIS, UTF8); break;
 
    case 'j2s':
        convert(JIS, SJIS); break;
 
    case 'j2e':
        convert(JIS, EUC); break;
 
    case 'u2s':
        convert(UTF8, SJIS); break;
 
    case 'u2j':
        convert(UTF8, JIS); break;
 
    case 'u2e':
        convert(UTF8, EUC); break;
 
    default:
        exit("mbConvertEncoding.php: command not found.\n");
}
 
function convert($from, $to)
{
    while($stdin = fgets(STDIN, 4096)) {
        echo mb_convert_encoding($stdin, $to, $from);
    }
}
?>

mkSymLink.sh

#!/bin/sh

ln -s ~/bin/mbConvertEncoding.php ~/bin/e2u
ln -s ~/bin/mbConvertEncoding.php ~/bin/e2s
ln -s ~/bin/mbConvertEncoding.php ~/bin/e2j
ln -s ~/bin/mbConvertEncoding.php ~/bin/s2u
ln -s ~/bin/mbConvertEncoding.php ~/bin/s2j
ln -s ~/bin/mbConvertEncoding.php ~/bin/s2e
ln -s ~/bin/mbConvertEncoding.php ~/bin/j2u
ln -s ~/bin/mbConvertEncoding.php ~/bin/j2s
ln -s ~/bin/mbConvertEncoding.php ~/bin/j2e
ln -s ~/bin/mbConvertEncoding.php ~/bin/u2s
ln -s ~/bin/mbConvertEncoding.php ~/bin/u2j
ln -s ~/bin/mbConvertEncoding.php ~/bin/u2e

※. 改行コードは必ず LF で保存してください




programming/php/cli/mbconvertencoding.txt