Created
August 5, 2009 07:01
-
-
Save alexbeutel/162549 to your computer and use it in GitHub Desktop.
This file contains 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
<?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