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与已存在的Java应用程序集成
Oct 09 PHP
杏林同学录(一)
Oct 09 PHP
PHP正则表达式之定界符和原子介绍
Oct 05 PHP
PHP中使用socket方式GET、POST数据实例
Apr 02 PHP
php实现用已经过去多长时间的方式显示时间
Jun 05 PHP
详解WordPress中提醒安装插件以及隐藏插件的功能实现
Dec 25 PHP
详解PHP防止盗链防止迅雷下载的方法
Apr 26 PHP
PHPExcel 修改已存在Excel的方法
May 03 PHP
Laravel框架路由管理简单示例
May 07 PHP
laravel5.6实现数值转换
Oct 23 PHP
php数值计算num类简单操作示例
May 15 PHP
php框架知识点的整理和补充
Mar 01 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
咖啡知识 咖啡养豆要养多久 排气又是什么
2021/03/06 新手入门
PHP解码unicode编码的中文字符代码分享
2014/08/13 PHP
thinkphp3.2点击刷新生成验证码
2016/02/16 PHP
ext form 表单提交数据的方法小结
2008/08/08 Javascript
javascript变量作用域使用中常见错误总结
2013/03/26 Javascript
Google Map V3 绑定气泡窗口(infowindow)Dom事件实现代码
2013/04/26 Javascript
方便实用的jQuery checkbox复选框全选功能简单实例
2013/10/09 Javascript
JavaScript中合并数组的N种方法
2014/09/16 Javascript
jquery+css实现绚丽的横向二级下拉菜单-附源码下载
2015/08/23 Javascript
AngularJS的Filter的示例详解
2017/03/07 Javascript
angular4自定义表单控件[(ngModel)]的实现
2018/11/23 Javascript
微信小程序如何获取用户收货地址
2018/11/27 Javascript
基于Nuxt.js项目的服务端性能优化与错误检测(容错处理)
2019/10/23 Javascript
解决element-ui的下拉框有值却无法选中的情况
2020/11/07 Javascript
解决VueCil代理本地proxytable无效报错404的问题
2020/11/07 Javascript
vue 数据遍历筛选 过滤 排序的应用操作
2020/11/17 Javascript
python 布尔操作实现代码
2013/03/23 Python
Django学习笔记之Class-Based-View
2017/02/15 Python
Python+PIL实现支付宝AR红包
2018/02/09 Python
python运用pygame库实现双人弹球小游戏
2019/11/25 Python
使用python求斐波那契数列中第n个数的值示例代码
2020/07/26 Python
Python+Xlwings 删除Excel的行和列
2020/12/19 Python
CSS3样式linear-gradient的使用实例
2017/01/16 HTML / CSS
联想香港官方网站及网店:Lenovo香港
2018/04/13 全球购物
会展中心部门工作职责
2013/11/27 职场文书
公司会计主管岗位责任制
2014/03/01 职场文书
优乐美广告词
2014/03/14 职场文书
十佳护士先进事迹
2014/05/08 职场文书
管理建议书范文
2014/05/13 职场文书
党支部遵守党的政治纪律情况对照检查材料
2014/09/26 职场文书
领导班子四风问题个人对照检查材料
2014/10/04 职场文书
个人批评与自我批评总结
2014/10/17 职场文书
C3 线性化算法与 MRO之Python中的多继承
2021/10/05 Python
mysql数据插入覆盖和时间戳的问题及解决
2022/03/25 MySQL
MySQL慢查询中的commit慢和binlog中慢事务的区别
2022/06/16 MySQL
css让页脚保持在底部位置的四种方案
2022/07/23 HTML / CSS