gfy usually fucks up the code but here goes..
p.s. before using this , test it out on a test folder to get used to how it works first .
save the following as
doit.php
make changes as needed
Code:
<?php
require_once('fandr.php');
//folder to search and all subdirs
$path = "folder";
// logfile
$logFile = "/result.txt";
// find this
$find = "test";
//replace with this
$replace = "nast2code";
$obj = new TextSearch();
// extensions to search
$obj->setExtensions(array('html','txt'));
$obj->addExtension('htm');
$obj->setSearchKey("$find");
$obj->setReplacementKey("$replace");
$obj->startSearching($path);
$obj->showLog();
$obj->writeLogToFile($logFile);
?>
save the following as fandr.php
no editing needed
Code:
<?php
class TextSearch
{
var $extensions = array();
var $searchKey = '';
var $replacementKey = '';
var $caseSensitive = 0;
var $findAllExts = 1;
var $isReplacingEnabled = 0;
var $logString = '';
var $errorText = '';
var $totalFound = 0; //total matches
function setExtensions($extensions = array())
{
$this->extensions = $extensions;
if(sizeof($this->extensions))
{
$this->findAllExts = 0; //not all extensions
}
}//End of Method
function addExtension($extension)
{
array_push($this->extensions, $extension);
$this->findAllExts = 0; //not all extensions
}//End of function
function setSearchKey($searchKey, $caseSensitive = 0)
{
$this->searchKey = $searchKey;
if($caseSensitivity)
{
$this->caseSensitive = 1;
}
}//End of function
function setReplacementKey($replacementKey)
{
$this->replacementKey = $replacementKey;
$this->isReplacingEnabled = 1;
}//End of function
function startSearching($path)
{
$this->findDirFiles($path);
}//EO Method
function findDirFiles($path)
{
$dir = opendir ($path);
while ($file = readdir ($dir))
{
if (($file == ".") or ($file == ".."))
{
continue;
}
if (filetype ("$path/$file") == "dir")
{
$this->findDirFiles("$path/$file"); //recursive traversing here
}
elseif($this->matchedExtension($file)) //checks extension if we need to search this file
{
if(filesize("$path/$file"))
{
$this->searchFileData("$path/$file"); //search file data
}
}
} //End of while
closedir($dir);
}//EO Method
function findExtension($file)
{
return array_pop(explode(".",$file));
}//End of function
function matchedExtension($file)
{
if($this->findAllExts) //checks if all extensions are to be searched
{
return true;
}
elseif(sizeof(array_keys($this->extensions, $this->findExtension($file)))==1)
{
return true;
}
return false;
}//EO Method
function searchFileData($file)
{
$searchKey = preg_quote($this->searchKey);
if($this->caseSensitive)
{
$pattern = "/$searchKey/U";
}
else
{
$pattern = "/$searchKey/Ui";
}
$subject = file_get_contents($file);
$found = 0;
$found = preg_match_all($pattern, $subject, $matches, PREG_PATTERN_ORDER);
$this->totalFound +=$found;
if($found)
{
$foundStr = "Found in $found places";
$this->appendToLog($file, $foundStr);
}
if($this->isReplacingEnabled && $this->replacementKey && $found)
{
// $this->replacementKey = "$replace";
$outputStr = preg_replace($pattern, $this->replacementKey, $subject);
$foundStr = "Found in $found places";
$this->writeToFile($file, $outputStr);
$this->appendToLog($file, $foundStr, $this->replacementKey);
}
elseif($this->isReplacingEnabled && $this->replacementKey == '')
{
$this->errorText .= "Replacement Text is not defined\n";
$this->appendToLog($file, "Replacement Text is not defined", $this->replacementKey);
}
elseif(!found)
{
$this->appendToLog($file, "No matching Found", $this->replacementKey);
}
}//EO Method
function writeToFile($file, $data)
{
if(is_writable($file))
{
$fp = fopen($file, "w");
fwrite($fp, $data);
fclose($fp);
}
else
{
$this->errorText .= "Can not replace text. File $file is not writable. \nPlease make it writable\n";
}
}//EO Method
function appendToLog($file, $matchStr, $replacementKey = null)
{
if($this->logString == '')
{
$this->logString = " --- Searching for '".$this->searchKey."' --- \n";
}
if($replacementKey == null)
{
$this->logString .= "Searching File $file : " . $matchStr."\n";
}
else
{
$this->logString .= "Searching File $file : " . $matchStr.". Replaced by '$replacementKey'\n";
}
}//EO Method
function showLog()
{
$this->dBug("------ Total ".$this->totalFound." Matches Found -----");
$this->dBug(nl2br($this->logString));
if($this->errorText!='')
{
$this->dBug("------Error-----");
$this->dBug(nl2br($this->errorText));
}
}//EO Method
function writeLogToFile($file)
{
$fp = fopen($file, "wb") OR die("Can not open file <b>$file</b>");
fwrite($fp, $this->logString);
fwrite($fp, "\n------ Total ".$this->totalFound." Matches Found -----\n");
if($this->errorText!='')
{
fwrite($fp, "\n------Error-----\n");
fwrite($fp, $this->errorText);
}
fclose($fp);
}//EO Method
function dBug($dump)
{
echo "<pre>";
print_r($dump);
echo "</pre>";
}//EO Method
} //End of class
?>
step #3 make a file called result.txt place it in the dir with the other 2 files and chmod it to be writeable *optional
this will search all files of the extension you specify in doit.php and find and replace text..
some things to be carefull of..
if you have lots of files make sure your php doesnt cutoff scripts at 30 seconds ( default )
this will replace everything in the dir you specify AND all subdirectories
if you have a question , ask first