Contents Search File Contents PHP searching for directory text content code

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.