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求最大子序列和的算法实现

复制代码 代码如下: <?php //作者:遥远的期待 //QQ:15624575 //算法分析:1、必须是整数序列、2、如果整个序列不全是负数,最大子序列的第一项必须是正数,否则...

深入理解PHP之源码目录结构与功能说明

本文讲述了PHP源码目录结构与功能说明。分享给大家供大家参考,具体如下: PHP之所以能在web开发语言中排名靠前,不仅仅是因为语法简单,上手容易。我个人认为更多是因为其语言本身的:模块...

解析thinkphp import 文件内容变量失效的问题

解析thinkphp import 文件内容变量失效的问题

用TP 集成支付宝账户绑定功能时碰上个问题ORM 下有文件 config.class.php直接import()后 发现里面的变量无法使用  但确实是加载咯。。(在config...

PHP实现ftp上传文件示例

FTP上传是PHP实现的一个常见且非常重要的应用技巧,今天就来与大家分享一下PHP实现FTP上传文件的简单示例。希望对大家的PHP学习能带来一定的帮助。 主要代码如下: functi...

php实现对两个数组进行减法操作的方法

本文实例讲述了php实现对两个数组进行减法操作的技巧。分享给大家供大家参考。具体如下: 本代码传入两个数组A和B,返回A-B的结果,即挑选出存在于A,但不存在于B的元素 <&#...