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实现的简单压缩英文字符串的代码
Apr 24 PHP
PHP 开发环境配置(Zend Server安装)
Apr 28 PHP
PHP通过header实现文本文件下载的代码
Aug 08 PHP
php 中英文语言转换类代码
Aug 11 PHP
PHP图片处理类 phpThumb参数用法介绍
Mar 11 PHP
常用PHP框架功能对照表
Oct 23 PHP
PHP中static关键字以及与self关键字的区别
Jul 01 PHP
codeigniter实现get分页的方法
Jul 10 PHP
php基于curl重写file_get_contents函数实例
Nov 08 PHP
Laravel统计一段时间间隔的数据方法
Oct 09 PHP
深入解析PHP底层机制及相关原理
Dec 11 PHP
php png失真的原因及解决办法
Nov 17 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配置心得包含MYSQL5乱码解决
2006/11/20 PHP
php操作sqlserver关于时间日期读取的小小见解
2009/11/29 PHP
php基于openssl的rsa加密解密示例
2016/07/11 PHP
PHP错误处理函数register_shutdown_function使用示例
2017/07/03 PHP
PHP 命名空间和自动加载原理与用法实例分析
2020/04/29 PHP
用javascript实现的图片马赛克后显示并切换加文字功能
2007/04/21 Javascript
IE/FireFox具备兼容性的拖动代码
2007/08/13 Javascript
javascript 建设银行登陆键盘
2008/06/10 Javascript
jQuery简单获取键盘事件的方法
2016/01/22 Javascript
confirm确认对话框的实现方法总结
2016/06/17 Javascript
深入理解js generator数据类型
2016/08/16 Javascript
微信小程序 开发指南详解
2016/09/27 Javascript
jQuery实现导航回弹效果
2017/02/27 Javascript
nodejs入门教程六:express模块用法示例
2017/04/24 NodeJs
vue element-ui table表格滚动加载方法
2018/03/02 Javascript
如何在 JavaScript 中更好地利用数组
2018/09/27 Javascript
详释JavaScript执行环境与执行栈
2019/04/02 Javascript
JS自定义滚动条效果
2020/03/13 Javascript
Vuejs通过拖动改变元素宽度实现自适应
2020/09/02 Javascript
vue 二维码长按保存和复制内容操作
2020/09/22 Javascript
Vue + ts实现轮播插件的示例
2020/11/10 Javascript
Python实现字典去除重复的方法示例
2017/07/31 Python
Python使用numpy实现BP神经网络
2018/03/10 Python
基于python实现KNN分类算法
2020/04/23 Python
Python实现生成密码字典的方法示例
2019/09/02 Python
django-csrf使用和禁用方式
2020/03/13 Python
python自动脚本的pyautogui入门学习
2020/04/01 Python
python安装cx_Oracle和wxPython的方法
2020/09/14 Python
Lulu & Georgia官方网站:购买地毯、家具、抱枕、壁纸、床上用品等
2018/03/19 全球购物
HSRP的含义以及如何工作
2014/09/10 面试题
技校毕业生的自我评价
2013/12/27 职场文书
行政助理工作职责范本
2014/03/04 职场文书
计算机售后服务承诺书
2014/05/30 职场文书
党的群众路线批评与自我批评范文
2014/10/16 职场文书
感谢信格式范文
2015/01/22 职场文书
辞职信怎么写?
2019/05/21 职场文书