<?php
/**
* Class to manipulate function require to simulate UNIX grep command
*/
$count = 0;
static $fileCounter = 0;
$fileSearched = array();
$dir_arr = array() ;
$dir_file = array() ;
$arrayOfOccurrence = array();
$arrayOfFilenames = array();
class ClassForGrep {
private static $instance = 0;
/**
* returns an instance of the ClassForGrep object.
* @access public
* @static
*
*/
public static function getInstance() {
if (ClassForGrep::$instance === 0) {
ClassForGrep::$instance = new ClassForGrep();
}
return ClassForGrep::$instance;
}
public $classForGrep;
/**
* creates a new ClassForGrep object.
* @access protected
*/
public function __construct() {
return $this->classForGrep;
}
/**
* to create and array of extentions
*
*
* @param $separator string/char
* @param $filesWithExtentionsToBeSearched string
*
* @return array
*/
public function createArrayOfExtentions($separator,$filesWithExtentionsToBeSearched) {
$arrayOfExtentions = explode($separator,$filesWithExtentionsToBeSearched);
foreach($arrayOfExtentions as $items ) {
$newArrayOfExtentions[] = trim($items);
}
return $newArrayOfExtentions;
}
/**
*
* function to convert time in secs into mins
*
* @param $timeInSec int
*
* $return string
*/
public function convertSecToMins($timeInSec) {
if( $timeInSec < 60 ) {
if( $timeInSec > 1) {
$secString = "secs";
}
else {
$secString = "sec";
}
$timeTaken = "$timeInSec $secString";
}
else {
$seconds = ($timeInSec % 60);
$minutes = ($timeInSec/60);
$minutes = sprintf("%01.0f", $minutes);
if( $minutes > 1) {
$minString = "mins";
}
else {
$minString = "min";
}
if( $seconds > 1) {
$secString = "secs";
}
else {
$secString = "sec";
}
$timeTaken = "$minutes $minString $seconds $secString";
}
return $timeTaken;
}
/**
*
* function to replace last occurrences of the search string with
* the replacement string .
*
* @param $search mixed
* @param $replace mixed
* @param $subject mixed
*
* $return mixed
*/
public function last_str_replace($search, $replace, $subject) {
$positionOfLastString = strrpos($subject,$search);
$stingBeforeSearchSting = substr($subject, 0, $positionOfLastString)." ";
$stingAfterSearchSting = substr($subject, $positionOfLastString+1, strlen($subject));
return $newString = $stingBeforeSearchSting.$replace.$stingAfterSearchSting;
}
/**
*
* function to read a directory recursively and finds a string in all the files.
* it also returns the number of times a sting was found in a file
*
* @param $path string
*
* @return mixed
*/
public function read_dir($path) {
global $dir_arr,$dir_file,$dir_path,$ptr,$fileSearched,$searchString,$replaceString,$fileCounter,$arrayOfOccurrence,$arrayOfFilenames,$filesWithExtentionsToBeSearched,$newArrayOfExtentions;
$handle = @opendir($path);
while ($file = @readdir($handle) ) {
if ( $file != "." && $file != ".." && is_dir($path.$file)) {
$sub_dir = $path . $file . "/" ;
array_push($dir_arr,$file) ;
$this->read_dir($sub_dir);
}
elseif ($file != "." && $file != ".." && is_file($path.$file)) {
$sub_dir = $path . $file ;
$temp = explode(".",$file);
$ext = end($temp);
$file_path = $sub_dir;
$display_filename = str_replace($scan_dir, './',$file_path);
// start searching and replacing
// searches only files extention in the given array
if(in_array(trim($ext),$newArrayOfExtentions) ) {
$fileContents=file_get_contents($file_path);
if(substr_count($fileContents,$searchString)) {
array_push($arrayOfFilenames,$display_filename);
array_push($arrayOfOccurrence,substr_count($fileContents,$searchString));
$fileCounter++;
}
array_push($dir_file,$file) ;
}
}
}
return $fileCounter;
}
}
?>