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

Posted on 2009-05-15
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.


$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;



  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

Question by:hankknight
1 Comment

Accepted Solution

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



	$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);



Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

939 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now