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 模拟POST|GET操作实现代码
Jul 20 PHP
使用Linux五年积累的一些经验技巧
Jun 20 PHP
PHP按行读取、处理较大CSV文件的代码实例
Apr 09 PHP
win7计划任务定时执行PHP脚本设置图解
May 09 PHP
Parse正式发布开源PHP SDK
Aug 11 PHP
php获取CSS文件中图片地址并下载到本地的方法
Dec 02 PHP
php提取身份证号码中的生日日期以及验证是否为成年人的函数
Sep 29 PHP
PHP 数组遍历foreach语法结构及实例
Jun 13 PHP
PHP读MYSQL中文乱码的快速解决方法
Oct 01 PHP
简单实现PHP留言板功能
Dec 21 PHP
YII框架页面缓存操作示例
Apr 29 PHP
php7 图形用户界面GUI 开发示例
Feb 22 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批量删除cookie的简单实现方法
2015/01/26 PHP
常用的php图片处理类(水印、等比缩放、固定高宽)分享
2015/06/19 PHP
thinkPHP框架整合tcpdf插件操作示例
2018/08/07 PHP
PHP7导出Excel报ERR_EMPTY_RESPONSE解决方法
2019/04/16 PHP
php的单例模式及应用场景详解
2021/02/27 PHP
getElementsByTagName vs selectNodes效率 及兼容的selectNodes实现
2010/02/26 Javascript
浅谈javascript语法和定时函数
2015/05/03 Javascript
浅谈JavaScript的Polymer框架中的behaviors对象
2015/07/29 Javascript
原生JS实现几个常用DOM操作API实例
2017/01/19 Javascript
Angular.JS利用ng-disabled属性和ng-model实现禁用button效果
2017/04/05 Javascript
Angular 4 指令快速入门教程
2017/06/07 Javascript
webpack将js打包后的map文件详解
2018/02/22 Javascript
微信小程序使用gitee进行版本管理
2018/09/20 Javascript
Vue cli3 库模式搭建组件库并发布到 npm的流程
2018/10/12 Javascript
基于JS实现web端录音与播放功能
2019/04/17 Javascript
深入讲解Java编程中类的生命周期
2016/02/05 Python
Python模块包中__init__.py文件功能分析
2016/06/14 Python
Python使用win32com实现的模拟浏览器功能示例
2017/07/13 Python
python实现整数的二进制循环移位
2019/03/08 Python
探秘TensorFlow 和 NumPy 的 Broadcasting 机制
2020/03/13 Python
详解CSS3 弹性布局快速入门
2019/06/06 HTML / CSS
如何使用canvas绘制可移动网格的示例代码
2020/12/14 HTML / CSS
日本运动品牌美津浓官方购物网站:MIZUNO SHOP
2016/08/21 全球购物
iRobot官网:改变生活的家用机器人品牌
2016/09/20 全球购物
纽约手袋品牌:KARA
2018/03/18 全球购物
MySQL面试题目集锦
2016/04/14 面试题
关于逃课的检讨书
2014/01/23 职场文书
党支部书记岗位责任制
2014/02/11 职场文书
上课打牌的检讨书
2014/02/15 职场文书
地方白酒代理协议书
2014/10/25 职场文书
聘任合同书
2015/09/21 职场文书
《雪域豹影》读后感:父爱的伟大
2019/12/23 职场文书
python源码剖析之PyObject详解
2021/05/18 Python
zabbix监控mysql的实例方法
2021/06/02 MySQL
Redis基本数据类型List常用操作命令
2022/06/01 Redis
MySQL运行报错:“Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre”解决方法
2022/06/14 MySQL