Created
April 26, 2012 01:14
-
-
Save ha1t/2494993 to your computer and use it in GitHub Desktop.
[いにしえのスクリプト]指定されたディレクトリの中全部のファイルの内容にたいしてstr_replaceするスクリプトっぽい!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/** | |
* replace_files | |
* | |
* @example replace_files.php before after * | |
*/ | |
if ($argc < 4) { | |
exit('invalid arguments'); | |
} | |
$basedir = dirname(__FILE__) . '/'; | |
$arg_list = $argv; | |
$my_name = array_shift($arg_list); | |
$before = array_shift($arg_list); | |
$after = array_shift($arg_list); | |
replace_files($before, $after, $arg_list); | |
function replace_files($before, $after, $arg_list) | |
{ | |
foreach ($arg_list as $file) { | |
if (is_dir($file)) { | |
replace_files($before, $after, glob(trim($file) . '/*')); | |
continue; | |
} | |
if (!file_exists($file)) { | |
$file = $basedir . $file; | |
} | |
if (!file_exists($file)) { | |
exit("File not found:{$file}"); | |
} | |
$data = file_get_contents($file); | |
$data2 = str_replace($before, $after, $data); | |
if (strcmp($data, $data2) !== 0) { | |
file_put_contents($file, $data2); | |
echo "replaced {$file}" . PHP_EOL; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment