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


Posted in PHP onFebruary 21, 2010

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

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

<?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编程最快明白》第六讲:Mysql数据库操作
Nov 01 PHP
PHP运行出现Notice : Use of undefined constant 的完美解决方案分享
Mar 05 PHP
php daddslashes()和 saddslashes()有哪些区别分析
Oct 26 PHP
PHP远程采集图片详细教程
Jul 01 PHP
destoon实现底部添加你是第几位访问者的方法
Jul 15 PHP
PHP永久登录、记住我功能实现方法和安全做法
Apr 27 PHP
PHP统计目录中文件以及目录中目录大小的方法
Jan 09 PHP
详解PHP的Yii框架中组件行为的属性注入和方法注入
Mar 18 PHP
PHP在线打包下载功能示例
Oct 15 PHP
PHP编程计算两个时间段是否有交集的实现方法(不算边界重叠)
May 30 PHP
php7新特性的理解和比较总结
Apr 14 PHP
PHP数组与字符串互相转换实例
May 05 PHP
php中理解print EOT分界符和echo EOT的用法区别小结
Feb 21 #PHP
用Zend Encode编写开发PHP程序
Feb 21 #PHP
PHP 学习路线与时间表
Feb 21 #PHP
php 高效率写法 推荐
Feb 21 #PHP
php 魔术函数使用说明
Feb 21 #PHP
php microtime获取浮点的时间戳
Feb 21 #PHP
PHP+ajax 无刷新删除数据
Feb 20 #PHP
You might like
PHP 数字左侧自动补0
2008/03/31 PHP
php用xpath解析html的代码实例讲解
2019/02/14 PHP
phpcmsv9.0任意文件上传漏洞解析
2020/10/20 PHP
js页面跳转常用的几种方式
2010/11/25 Javascript
jquery动态添加删除div 具体实现
2013/07/20 Javascript
浅谈angularJS 作用域
2015/07/05 Javascript
JavaScript中字符串与Unicode编码互相转换的实现方法
2015/12/18 Javascript
Bootstrap实现导航栏的2种方式
2016/11/28 Javascript
@ResponseBody 和 @RequestBody 注解的区别
2017/03/08 Javascript
详解如何使用koa实现socket.io官网的例子
2018/11/04 Javascript
JS Math对象与Math方法实例小结
2019/07/05 Javascript
JQuery实现ul中添加LI和删除指定的Li元素功能完整示例
2019/10/16 jQuery
node事件循环和process模块实例分析
2020/02/14 Javascript
使用vue构建多页面应用的示例
2020/10/22 Javascript
详解JavaScript中的链式调用
2020/11/27 Javascript
[46:47]2014 DOTA2国际邀请赛中国区预选赛5.21 LGD-CDEC VS NE
2014/05/22 DOTA
[47:55]Ti4第二日主赛事败者组 NaVi vs EG 1
2014/07/20 DOTA
python中django框架通过正则搜索页面上email地址的方法
2015/03/21 Python
python实现微信跳一跳辅助工具步骤详解
2018/01/04 Python
使用Python实现在Windows下安装Django
2018/10/17 Python
Django用户身份验证完成示例代码
2020/04/03 Python
HTML5 Canvas绘制文本及图片的基础教程
2016/03/14 HTML / CSS
Html5调用手机摄像头并实现人脸识别的实现
2018/12/21 HTML / CSS
英国绿色商店:Natural Collection
2019/05/03 全球购物
PHP开发工程师面试问题集锦
2012/11/01 面试题
期末自我鉴定
2014/01/23 职场文书
护理专业自我鉴定
2014/01/30 职场文书
2014年创卫实施方案
2014/02/18 职场文书
还款承诺书范文
2014/05/20 职场文书
新疆民族团结演讲稿
2014/08/27 职场文书
信用卡工资证明格式
2014/09/13 职场文书
2015年全民国防教育日活动总结
2015/03/23 职场文书
2019年怎样才能撰写出优秀的自荐信
2019/03/25 职场文书
学习nginx基础知识
2021/09/04 Servers
Springboot/Springcloud项目集成redis进行存取的过程解析
2021/12/04 Redis
python读取并查看npz/npy文件数据以及数据显示方法
2022/04/14 Python