This handy function will search and replace all instances of a search term in every file with the PHP extension, across all directories and subdirectories. Upload the file to your root directory and run it.
function callbackDir($dir){
$searchString = "searchforthis";
$newValue = "replacewiththis";
// directory, and file type
$path = "$dir/*.php";
$searchString = "#".$searchString."#";
$globarray = glob($path);
if ($globarray) foreach ($globarray as $filename) {
$source = file_get_contents($filename);
if (preg_match($searchString,$source)) echo "$filename <br>";
$source = preg_replace($searchString,$newValue,$source);
file_put_contents($filename,$source);
$count++;
}
echo "Done - processed $count files";
echo "$dir<br />";
}
function walkDir($dir,$fx)
{
$arStack = array();
$fx($dir);
if( ($dh=opendir($dir)) )
{ while( ($file=readdir($dh))!==false )
{ if( $file=='.' || $file=='..' ) continue;
if( is_dir("$dir/$file") )
{ if( !in_array("$dir/$file",$arStack) ) $arStack[]="$dir/$file";
}
}
closedir($dh);
}
if( count($arStack) )
{ foreach( $arStack as $subdir )
{ walkDir($subdir,$fx);
}
}
}
walkDir('./',callBackDir);
Leave A Comment