This class can be used to search for files in a given text directory.
It can be given a directory traversal recursively looking up files for some file name extensions.
and open the found files and check if they contain search terms.
It returns a list of all the files containing the search word array.
Copy the Code code as follows:
/*
Class for searching the contents of all the files in a directory and its subdirectories
For support Visit http://www.webdigity.com/
*/
Class searchfilecontents{
var $dir _name = ";//the directory to search
var $search _phrase = ";//the phrase to search in the file contents
var $allowed _file_types = Array (' php ', ' Phps ');//the file types that is searched
var $foundFiles;//files that contain the search phrase'll be a stored here
Open Source Code OSPHP.COM.Cn
var $myfiles;
function Search ($directory, $search _phrase) {
$this->dir_name = $directory;
$this->search_phrase = $search _phrase;
$this->myfiles = $this->getdircontents ($this->dir_name);
$this->foundfiles = Array ();
if (Empty ($this->search_phrase)) die (' empty search phrase ');
if (Empty ($this->dir_name)) Die (' must select a directory to search ');
foreach ($this->myfiles as $f) {
if (In_array (Array_pop (Explode ('. ', $f)), $this->allowed_file_types)) {//Open source OSPhP.COM.CN
$contents = file_get_contents ($f);
if (Strpos ($contents, $this->search_phrase)!== false)
$this->foundfiles [] = $f;
Open Source Code OSPhP.COm.CN
}
}
return $this->foundfiles;
}
function Getdircontents ($dir) {
if (!is_dir ($dir)) {die ("Function getdircontents:problem reading: $dir!");}
if ($root = @opendir ($dir)) {
PHP Open Source Code
while ($file =readdir ($root)) {
if ($file = = "." | | $file = = "..") {continue;}
if (Is_dir ($dir. " /". $file)) {
$files =array_merge ($files, $this->getdircontents ($dir. " /". $file));
}else{
$files []= $dir.] /". $file; Open Source OSPhP.COM.CN
}
}
}
return $files;
}
}
Example:
$search = new Searchfilecontents;
$search->search (' E:/htdocs/accessclass ', ' class '); Open Source Code OSPHP.COM.Cn
Var_dump ($search->foundfiles);
?>
The above describes the Contents search File Contents php Directory text content code, including the Contents aspect of the content, I hope to be interested in PHP tutorial friends helpful.