Solved

Recursive Find/Replace text in all config files (PHP)

Posted on 2009-05-15
1
283 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:hankknight
1 Comment
 
LVL 1

Accepted Solution

by:
mvanrooij earned 500 total points
ID: 24395888
If you have the full path to the file, use this function to update the file.

It takes the full path, the search string and the replace string as parameter.
function($sPathToFile,$sFind,$sReplace) {
	
	//get contents of file
	$contents = file_get_contents($sPathToFile);
	
	//replace
	$contentsNew = str_replace($sFind,$sReplace,$contents);
	
	//write back to the same file (overwrite)
	$fh = fopen($sPathToFile, 'w') or die("can't open file");
	fwrite($fh, $contentsNew);
	fclose($fh);
 
}

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question