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下使用SMTP发邮件的代码
Jan 10 PHP
php面向对象的方法重载两种版本比较
Sep 08 PHP
简单的php写入数据库类代码分享
Jul 26 PHP
php5.5新数组函数array_column使用
Jul 08 PHP
php var_export与var_dump 输出的不同
Aug 09 PHP
php检测apache mod_rewrite模块是否安装的方法
Mar 14 PHP
ThinkPHP V2.2说明文档没有说明的那些事实例小结
Jul 01 PHP
ThinkPHP连接Oracle数据库
Apr 22 PHP
PHP与Java对比学习日期时间函数
Jul 03 PHP
redis+php实现微博(一)注册与登录功能详解
Sep 23 PHP
PHP字符串和十六进制如何实现互相转换
Jul 16 PHP
如何使用php生成zip压缩包
Apr 21 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环境配置 php5 mysql5 apache2 phpmyadmin安装与配置
2006/11/17 PHP
Thinkphp中import的几个用法详细介绍
2014/07/02 PHP
推荐几款用 Sublime Text 开发 Laravel 所用到的插件
2014/10/30 PHP
Laravel 5框架学习之用户认证
2015/04/09 PHP
Yii的Srbac插件用法详解
2016/07/14 PHP
thinkPHP5框架分页样式类完整示例
2018/09/01 PHP
通过event对象的fromElement属性解决热区设置主实体的一个bug
2008/12/22 Javascript
jQuery数据显示插件整合实现代码
2011/10/24 Javascript
Javascript中Event属性搜集整理
2013/09/17 Javascript
javascript限制用户只能输汉字中文的方法
2014/11/20 Javascript
js实现class样式的修改、添加及删除的方法
2015/01/20 Javascript
jquery.mousewheel实现整屏翻屏效果
2015/08/30 Javascript
Node.js的Web模板引擎ejs的入门使用教程
2016/06/06 Javascript
JS控制静态页面传递参数并获取参数应用
2016/08/10 Javascript
简单实现JS倒计时效果
2016/12/23 Javascript
原生js实现秒表计时器功能
2017/02/16 Javascript
Bootstrap一款超好用的前端框架
2017/09/25 Javascript
动态加载JavaScript文件的3种方式
2018/05/05 Javascript
layui 实现表格某一列显示图标
2019/09/19 Javascript
jdk1.8+vue elementui实现多级菜单功能
2020/09/24 Javascript
python实现排序算法
2014/02/14 Python
python正则表达式中的括号匹配问题
2014/12/14 Python
Python3通过Luhn算法快速验证信用卡卡号的方法
2015/05/14 Python
使用Python来开发微信功能
2018/06/13 Python
Python 从subprocess运行的子进程中实时获取输出的例子
2019/08/14 Python
pytorch中交叉熵损失(nn.CrossEntropyLoss())的计算过程详解
2020/01/02 Python
Pytorch之扩充tensor的操作
2021/03/04 Python
巴西婴儿用品商店:Bebe Store
2017/11/23 全球购物
三分钟演讲稿范文
2014/04/24 职场文书
社区爱国卫生月活动总结
2014/06/30 职场文书
党性观念心得体会
2014/09/03 职场文书
鲁迅故里导游词
2015/02/05 职场文书
会计求职简历自我评价
2015/03/10 职场文书
2015暑期社会实践通讯稿
2015/07/18 职场文书
导游词之丹东鸭绿江
2019/10/24 职场文书
基于Pygame实现简单的贪吃蛇游戏
2021/12/06 Python