This code :
- Takes path to search files within
- Takes replacement key if you want to replace matching results by any text
- Traverses files within the directory( and subdirectories as well) specified
- Searches the specified search key within the files by regular expression
- Writes matched filename and number of occurences
- If replacementKey is given, it replaces the matched texts by the replacementKey using regular expression
- Writes results to a log file (if specified any) as well as standard display
TextSearch.class.php
<?php
/**
* Class : TextSearch
*
* @author : MA Razzaque Rupom <rupom_315@yahoo.com>, <rupom.bd@gmail.com>
* Moderator, phpResource Group(http://groups.yahoo.com/group/phpresource/)
* URL: http://www.rupom.info
*
* @version : 1.0
* Date : 06/26/2006
* Purpose : Searching and replacing text within files of specified path
*/
class TextSearch
{
var $extensions = array();
var $searchKey = '';
var $replacementKey = '';
var $caseSensitive = 0; //by default case sensitivity is OFF
var $findAllExts = 1; //by default all extensions
var $isReplacingEnabled = 0;
var $logString = '';
var $errorText = '';
var $totalFound = 0; //total matches
/**
* Wrapper function around function findDirFiles()
* @param $path to search
* @return none
*/
function startSearching($path)
{
$this->findDirFiles($path);
}//EO Method
/**
* Recursively traverses files of a specified path
* @param path to execute
* @return none
*/
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
/**
* Finds extension of a file
* @param filename
* @return file extension
*/
function findExtension($file)
{
return array_pop(explode(".",$file));
}//End of function
/**
* Checks if a file extension is one the extensions we are going to search
* @param filename
* @return true in success, false otherwise
*/
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
/**
* Searches data, replaces (if enabled) with given key, prepares log
* @param $file
* @return none
*/
function searchFileData($file)
{
$searchKey = preg_quote($this->searchKey);
}
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
/**
* Writes new data (after the replacement) to file
* @param $file, $data
* @return none
*/
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
/**
* Appends log data to previous log data
* @param filename, match string, replacement key if any
* @return none
*/
function appendToLog($file, $matchStr, $replacementKey = null)
{
if($this->logString == '')
{
$this->logString = " --- Searching for '".$this->searchKey."' --- \n";
}
$obj = new TextSearch();
$obj->setExtensions(array('html','txt')); //setting extensions to search files within
$obj->addExtension('php');//adding an extension
$obj->setSearchKey(' PHP');
$obj->setReplacementKey('phpResource');//setting replacement text if you want to replace matches with that
$obj->startSearching($path);//starting search
$obj->showLog();//showing log
$obj->writeLogToFile($logFile); //writting result to log file