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 相关文章推荐
模仿OSO的论坛(三)
Oct 09 PHP
php生成的html meta和link标记在body标签里 顶部有个空行
May 18 PHP
PHP调用VC编写的COM组件实例
Mar 29 PHP
PHP下通过QRCode类库创建中间带网站LOGO的二维码
Jul 12 PHP
Windows下的PHP安装pear教程
Oct 24 PHP
php基于GD库画五星红旗的方法
Feb 24 PHP
PHP SESSION的增加、删除、修改、查看操作
Mar 20 PHP
PHP加密解密类实例分析
Apr 20 PHP
简单了解WordPress开发中update_option()函数的用法
Jan 11 PHP
Yii统计不同类型邮箱数量的方法
Oct 18 PHP
微信小程序发送订阅消息的方法(php 为例)
Oct 30 PHP
php让json_encode不自动转义斜杠“/”的方法
Apr 27 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引用
2015/02/22 PHP
jqGrid jQuery 表格插件测试代码
2011/08/23 Javascript
html中的input标签的checked属性jquery判断代码
2012/09/19 Javascript
图片放大镜jquery.jqzoom.js使用实例附放大镜图标
2014/06/19 Javascript
javascript格式化json显示实例分析
2015/04/21 Javascript
JavaScript的Backbone.js框架环境搭建及Hellow world示例
2016/05/07 Javascript
Bootstrap源码解读导航(6)
2016/12/23 Javascript
浅谈Angular2 模块懒加载的方法
2017/10/04 Javascript
基于JavaScript实现幸运抽奖页面
2020/07/05 Javascript
vue-router命名路由和编程式路由传参讲解
2019/01/19 Javascript
vue使用codemirror的两种用法
2019/08/27 Javascript
python应用程序在windows下不出现cmd窗口的办法
2014/05/29 Python
在Python程序中进行文件读取和写入操作的教程
2015/04/28 Python
python相似模块用例
2016/03/04 Python
Python实现简单过滤文本段的方法
2017/05/24 Python
Python 中导入csv数据的三种方法
2018/11/01 Python
python selenium 弹出框处理的实现
2019/02/26 Python
不到40行代码用Python实现一个简单的推荐系统
2019/05/10 Python
使用python爬取抖音视频列表信息
2019/07/15 Python
分享一个pycharm专业版安装的永久使用方法
2019/09/24 Python
详解使用django-mama-cas快速搭建CAS服务的实现
2019/10/30 Python
python使用HTMLTestRunner导出饼图分析报告的方法
2019/12/30 Python
python图片指定区域替换img.paste函数的使用
2020/04/09 Python
安装pyinstaller遇到的各种问题(小结)
2020/11/20 Python
CSS3 中的@keyframes介绍
2014/09/02 HTML / CSS
CSS3实现曲线阴影和翘边阴影
2016/05/03 HTML / CSS
HTML5中使用postMessage实现Ajax跨域请求的方法
2016/04/19 HTML / CSS
HTML5中indexedDB 数据库的使用实例
2017/05/11 HTML / CSS
施华洛世奇加拿大官网:SWAROVSKI加拿大
2018/06/03 全球购物
电子商务专业个人的自我评价分享
2013/10/29 职场文书
原料仓管员岗位职责
2014/04/12 职场文书
婚前协议书标准版
2014/10/19 职场文书
优秀班主任工作总结2015
2015/05/25 职场文书
征求意见函
2015/06/05 职场文书
2016年寒假家长评语
2015/10/10 职场文书
一文简单了解MySQL前缀索引
2022/04/03 MySQL