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 上一篇,下一篇文章实现代码与原理说明
May 09 PHP
php中用数组的方法设置cookies
Apr 21 PHP
php实现rc4加密算法代码
Apr 25 PHP
php中防止恶意刷新页面的代码小结
Oct 31 PHP
php class中self,parent,this的区别以及实例介绍
Apr 24 PHP
解析PHP中的内存管理,PHP动态分配和释放内存
Jun 28 PHP
WordPress导航菜单的滚动和淡入淡出效果的实现要点
Dec 14 PHP
laravel多条件查询方法(and,or嵌套查询)
Oct 09 PHP
PHP实现新型冠状病毒疫情实时图的实例
Feb 04 PHP
gearman中worker常驻后台,导致MySQL server has gone away的解决方法
Feb 27 PHP
深入分析PHP设计模式
Jun 15 PHP
PHP中SESSION过期设置
Mar 09 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
操作Oracle的php类
2006/10/09 PHP
PHP图片上传类带图片显示
2006/11/25 PHP
PHP基于GD库的缩略图生成代码(支持jpg,gif,png格式)
2014/06/19 PHP
php实现文件上传及头像预览功能
2017/01/15 PHP
javascript下阻止表单重复提交、防刷新、防后退
2007/08/17 Javascript
javascript Excel操作知识点
2009/04/24 Javascript
基于jQuery的表格操作插件
2010/04/22 Javascript
基于jquery的不规则矩形的排列实现代码
2012/04/16 Javascript
js简单实现HTML标签Select联动带跳转
2013/10/23 Javascript
JS兼容浏览器的导出Excel(CSV)文件的方法
2014/05/03 Javascript
jquery 获取 outerHtml 包含当前节点本身的代码
2014/10/30 Javascript
JS实现自适应高度表单文本框的方法
2015/02/25 Javascript
jQuery DOM插入节点操作指南
2015/03/03 Javascript
JavaScript实现控制打开文件另存为对话框的方法
2015/04/17 Javascript
基于JavaScript实现div层跟随滚动条滑动
2016/01/12 Javascript
JS实现控制文本框的内容
2016/07/10 Javascript
javascript表单控件实例讲解
2016/09/13 Javascript
浅谈js函数的多种定义方法与区别
2016/11/29 Javascript
微信小程序 数组中的push与concat的区别
2017/01/05 Javascript
关于jQuery EasyUI 中刷新Tab选项卡后一个页面变形的解决方法
2017/03/02 Javascript
vue下history模式刷新后404错误解决方法
2018/08/18 Javascript
使用Vue.observable()进行状态管理的实例代码详解
2019/05/26 Javascript
Vue 页面权限控制和登陆验证功能的实例代码
2019/06/20 Javascript
JS实现骰子3D旋转效果
2019/10/24 Javascript
[01:00:52]2018DOTA2亚洲邀请赛 4.4 淘汰赛 EG vs LGD 第一场
2018/04/05 DOTA
跟老齐学Python之复习if语句
2014/10/02 Python
Python实现登录接口的示例代码
2017/07/21 Python
Django教程笔记之中间件middleware详解
2018/08/01 Python
Python字典的概念及常见应用实例详解
2019/10/30 Python
python字符串判断密码强弱
2020/03/18 Python
Python openpyxl模块实现excel读写操作
2020/06/30 Python
Groupon荷兰官方网站:高达70%的折扣
2019/11/01 全球购物
国培远程培训感言
2014/03/08 职场文书
2014年调度员工作总结
2014/11/19 职场文书
2015个人半年总结范文
2015/03/09 职场文书
因公司原因离职的辞职信范文
2015/05/12 职场文书