hankknight
asked on
Recursive Find/Replace text in all config files (PHP)
I want to recursively find all instances of ###foobar### and replace them with ###ZAPHU### in all files named '_config.php'
I have attached some code that does the first part-- it finds the config files. Now I need to do the replacement.
I have attached some code that does the first part-- it finds the config files. Now I need to do the replacement.
<?php
$sitesDir = '/home/mydir/';
function my_handler($filename) {
echo $filename . "\n<br/>";
}
find_files($sitesDir, '/_config.php$/', 'my_handler');
function find_files($path, $pattern, $callback) {
$path = rtrim(str_replace("\\", "/", $path), '/') . '/';
$matches = Array();
$entries = Array();
$dir = dir($path);
while (false !== ($entry = $dir->read())) {
$entries[] = $entry;
}
$dir->close();
foreach ($entries as $entry) {
$fullname = $path . $entry;
if ($entry != '.' && $entry != '..' && is_dir($fullname)) {
find_files($fullname, $pattern, $callback);
} else if (is_file($fullname) && preg_match($pattern, $entry)) {
call_user_func($callback, $fullname);
}
}
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.