Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

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.


<?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);
    }
  }
}
 
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mvanrooij
mvanrooij
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial