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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 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

14 Experts available now in Live!

Get 1:1 Help Now