Skip to content

Instantly share code, notes, and snippets.

@alexbeutel
Created August 5, 2009 07:01
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 alexbeutel/162549 to your computer and use it in GitHub Desktop.
Save alexbeutel/162549 to your computer and use it in GitHub Desktop.
<?php
$find = array("findWord1", "findWord2");
$replace = array("replaceWord1", "replaceWord2");
// wouldn't suggest keeping the file in the same directory as being search
// so make the line below a string of an absolute folder location
$home = getcwd();
$all = array();
function bfs($path) {
global $all;
$temp = array();
if($h = opendir($path)) {
while(($f = readdir($h)) != false) {
if($f == "." || $f == "..") continue;
if(is_dir($path."/".$f)) array_push($temp, $f);
array_push($all, $path."/".$f);
}
closedir($h);
while(($f = array_pop($temp)) != NULL) {
bfs($path."/".$f);
}
}
}
bfs($home);
while(($el = array_pop($all))!= NULL) {
$i = strrpos($el, "/");
$last = str_replace($find, $replace, substr($el, $i));
$name = substr($el, 0, $i).$last;
if($name != $el) {
echo $el."\n".$name."\n\n";
rename($el, $name);
}
if(!is_dir($name)) {
$fh = fopen($name, 'r');
$data = fread($fh, filesize($name));
fclose($fh);
$new_data = str_replace($find, $replace, $data);
if($data != $new_data && filesize($name) != 0) {
echo "REWRITE: ".$name."\n";
$fh = fopen($name, 'w');
fwrite($fh, $new_data);
fclose($fh);
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment