Skip to content

Instantly share code, notes, and snippets.

@hayakawa
Created February 16, 2015 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayakawa/87357cf69b75b8b59d7d to your computer and use it in GitHub Desktop.
Save hayakawa/87357cf69b75b8b59d7d to your computer and use it in GitHub Desktop.
<?php
// 置換元と置換先のマップは配列で用意
$search = array("が","ぎ","ぐ","げ","ご","ざ","じ","ず","ぜ","ぞ","だ","ぢ","づ","で","ど","ば","び","ぶ","べ","ぼ","ぱ","ぴ","ぷ","ぺ","ぽ","ガ","ギ","グ","ゲ","ゴ","ザ","ジ","ズ","ゼ","ゾ","ダ","ヂ","ヅ","デ","ド","バ","ビ","ブ","ベ","ボ","パ","ピ","プ","ペ","ポ");
$replace = array("が","ぎ","ぐ","げ","ご","ざ","じ","ず","ぜ","ぞ","だ","ぢ","づ","で","ど","ば","び","ぶ","べ","ぼ","ぱ","ぴ","ぷ","ぺ","ぽ","ガ","ギ","グ","ゲ","ゴ","ザ","ジ","ズ","ゼ","ゾ","ダ","ヂ","ヅ","デ","ド","バ","ビ","ブ","ベ","ボ","パ","ピ","プ","ペ","ポ");
// 引数が適正かをチェック
if ($argc > 1 and !empty($argv[1]) and is_dir($argv[1]) and is_writable($argv[1])) {
// 引数値を正規化
$dirpath = str_replace('//', '/', trim($argv[1] . '/'));
// ファイルキャッシュクリア後に指定ディレクトリ内を走査
clearstatcache();
if ($dir = opendir($dirpath)) {
// 対象ディレクトリ中の全ファイルを走査
while (($file = readdir($dir)) !== false) {
// 対象ファイルの場合のみ処理
if (is_file($dirpath . $file) and is_readable($dirpath . $file) and fnmatch('*.m3u', $file)) {
// ファイルの中身を取得し変換(オリジナルファイルはリネームする)
if (($content = file_get_contents($dirpath . $file)) !== false and !empty($content)) {
echo "Converting: " . $dirpath . $file . PHP_EOL;
rename($dirpath . $file, str_replace('.m3u', '.m3u_original', $dirpath . $file));
$new_content = str_replace($search, $replace, mb_convert_encoding(strtr($content, array_fill_keys(array("\r\n", "\r", "\n"), "\r\n")), 'UTF-8', 'auto'));
echo "Convert: " . ((file_put_contents($dirpath . $file, $new_content) !== false) ? "SUCCEEDED" : "FAILED") . PHP_EOL;
}
}
}
closedir($dir);
}
} else {
// 引数値が予期せぬものだった場合は、Usage表示
$usage_text =<<<EOT
[USAGE]
$argv[0] {directory path which playlists included}
[NOTES]
* Directory path MUST be a writable.
* The files extension MUST be a "m3u".
* The converted files are NOT over write.(Original files are renamed to "*.m3u_original")
EOT;
echo $usage_text . PHP_EOL;
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment