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

yipeiwu_com6年前PHP代码库
这个类可以用来搜索在给定的文本目录中的文件。
它可以给定目录遍历递归查找某些文件扩展名的文件。
并打开找到的文件,并检查他们是否包含搜索词语。

它返回一个含有所有文件的列表包含搜索词语数组。

复制代码 代码如下:

<?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);
?>

相关文章

PHP最常用的正则表达式

一、校验数字的表达式 数字:^[0-9]*$ n位的数字:^\d{n}$ 至少n位的数字:^\d{n,}$ m-n位的数字:^\d{m,n}$ 零和非零开头的数字:^(0|[1-9][0...

列举PHP的Yii 2框架的开发优势

列举PHP的Yii 2框架的开发优势

当 Yii框架仍处于 RC(候选版)阶段时,我们 对它进行过报道,那时它刚刚全面达到候选版本阶段,(现在它已经发布了正式版本)我们感觉是时候再次讨论这个话题:选择 Yii框架的原因。 1...

PHP 递归效率分析

而且是差了3倍的效率。所以,PHP中的递归一定要小心的对待。 最近写了一个快速排序的算法,发现PHP中的递归效率不能一刀切,在各种不同的服务器中,可能会表现不一样。 复制代码 代码如下:...

SESSION信息保存在哪个文件目录下以及能够用来保存什么类型的数据

1.SESSION信息保存在哪? 复制代码 代码如下: <?php session_start(); $_SESSION['name']='marcofly'; ?> se...

用php实现让页面只能被百度gogole蜘蛛访问的方法

普通用户与搜索引擎蜘蛛爬行的区别在于发送的user agent,看网站日志文件能发现百度蜘蛛名字包含Baiduspider, 而google的则是Googlebot, 这样我们可以通过判...