Search File Contents The code _php skills of the text content of the PHP searching directory

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 find files for certain file extensions.
And open the files found and check to see if they contain search terms.

It returns a list of all files containing an array of search words.

Copy Code code as follows:

<?php
/*
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 are searched
var $foundFiles//files that contain the search phrase is 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 (' You 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);
?>

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.