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

yipeiwu_com5年前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序列化操作方法。分享给大家供大家参考,具体如下: 序列化就是将变量数据转换为字符串(跟类型转换机制不同),一般应用于存储数据(文件),然后在别的情形下恢复(反序列化)...

php下统计用户在线时间的一种尝试

下面列出几个比较常用的方法: 首先介绍一下所涉及的数据表结构,四个字段: 复制代码 代码如下: uid<int(10)> :用户id session_id<varcha...

php中邮箱地址正则表达式实现与详解

首先附上代码 复制代码 代码如下: ^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$ 在这段正则表达式中,“+”表示前面的字符串连续出现一...

ThinkPHP与PHPExcel冲突解决方法

很早之前就知道有一个叫做PHPExcel的类(官方网站)可以用来操作Excel,一直没有机会尝试,今天试用发现无比强大,下载后的源码包里有详细文档,几乎能实现手工操作Excel能实现的一...

一个好用的PHP分页函数

网友原创的代码,高手看来,也许流程笨拙点,但是很实用.看者要顶啊       /*----------------------------...