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

相关文章

require(),include(),require_once()和include_once()的异同

require()和include()有许多相似之处,也有些不同。理解它们的不同点非常重要,否则很容易犯错误。  我把这两个语句放在一起介绍,读者可以比较学习。  1...

PHP编程实现的TCP服务端和客户端功能示例

本文实例讲述了PHP编程实现的TCP服务端和客户端功能。分享给大家供大家参考,具体如下: 1、修改php.ini,打开extension=php_sockets.dll 2、服务端程序S...

PHP中的Session对象如何使用

在PHP开发中对比起Cookie,session 是存储在服务器端的会话,相对安全,并且不像 Cookie 那样有存储长度限制。下面则是对Session的介绍。 php中的Session...

什么是OneThink oneThink后台添加插件步骤

什么是OneThink oneThink后台添加插件步骤

OneThink以其便捷的建站、丰富的扩展、灵活的二次开发,以及云服务的支持,为广大个人和企业建站带来新的契机和机遇,即将成为互联网新的弄潮儿。 OneThink特性介绍: 1. 基于T...

PHP版国家代码、缩写查询函数代码

复制代码 代码如下: <?php function transCountryCode($code) { $index=array('AA'=>'阿鲁巴', 'AD'=>...