Search File Contents PHP 搜索目录文本内容的代码

这个类可以用来搜索在给定的文本目录中的文件
它可以给定目录遍历递归查找某些文件扩展名的文件
并打开找到的文件,并检查他们是否包含搜索词语。 它返回一个含有所有文件的列表包含搜索词语数组。 <div class="codetitle"><a style="CURSOR: pointer" data="11684" class="copybut" id="copybut11684" onclick="doCopy('code11684')"> 代码如下:

<div class="codebody" id="code11684">
<?PHP
/
Class for searching the contents of all the files in a directory and its subdirectories
For support please 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 will be stored here
//开源代码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) ){ //开源OSPHP.COM.CN
$contents = file_get_contents($f);
if ( strpos($contents,$this->search_phrase) !== false )
$this->foundFiles [] = $f;
//开源代码OSPHP.COm.CN }
}
return $this->foundFiles;
}
function GetDirContents($dir){
if (!is_dir($dir)){die ("Function GetDirContents: Problem reading : $dir!");}
if ($root=@opendir($dir)){
//PHP开源代码 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; //开源OSPHP.COM.CN
}
}
}
return $files;
}
}
//Example :
$search = new searchFileContents;
$search->search('E:/htdocs/AccessClass','class'); //开源代码OSPHP.COM.Cn
var_dump($search->foundFiles);
?>

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...