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 不错的学习资料
Feb 06 PHP
php str_pad 函数用法简介
Jul 11 PHP
str_replace只替换一次字符串的方法
Apr 09 PHP
解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)
Jul 03 PHP
php基础设计模式大全(注册树模式、工厂模式、单列模式)
Aug 31 PHP
100多行PHP代码实现socks5代理服务器[2]
May 05 PHP
PHP会员找回密码功能的简单实现
Sep 05 PHP
PHP使用Nginx实现反向代理
Sep 20 PHP
ThinkPHP框架使用redirect实现页面重定向的方法实例分析
Apr 12 PHP
PHP+redis实现的购物车单例类示例
Feb 02 PHP
ThinkPHP框架实现的微信支付接口开发完整示例
Apr 10 PHP
PHP代码覆盖率统计详解
Jul 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代码
2016/08/08 PHP
PHP获取类私有属性的3种方法
2020/09/10 PHP
破除一些网站复制、右键限制
2006/11/04 Javascript
javascript 关闭IE6、IE7
2009/06/01 Javascript
myFocus slide3D v1.1.0 使用方法与下载
2011/01/12 Javascript
实现网页页面跳转的几种方法(meta标签、js实现、php实现)
2014/05/20 Javascript
BootStrap Table 分页后重新搜索问题的解决办法
2016/08/08 Javascript
HTML中setCapture、releaseCapture 使用方法浅析
2016/09/25 Javascript
JS中this上下文对象使用方式
2016/10/09 Javascript
Bootstrap中datetimepicker使用小结
2016/12/28 Javascript
JavaScript仿微信打飞机游戏
2020/07/05 Javascript
解决vue里碰到 $refs 的问题的方法
2017/07/13 Javascript
bootstrap-table实现服务器分页的示例 (spring 后台)
2017/09/01 Javascript
node.js将MongoDB数据同步到MySQL的步骤
2017/12/10 Javascript
js事件on动态绑定数据,绑定多个事件的方法
2018/09/15 Javascript
python处理csv数据的方法
2015/03/11 Python
小米5s微信跳一跳小程序python源码
2018/01/08 Python
Python内置函数reversed()用法分析
2018/03/20 Python
Python元组知识点总结
2019/02/18 Python
python实现二级登陆菜单及安装过程
2019/06/21 Python
python中p-value的实现方式
2019/12/16 Python
python获取引用对象的个数方式
2019/12/20 Python
PythonPC客户端自动化实现原理(pywinauto)
2020/05/28 Python
python使用建议技巧分享(三)
2020/08/18 Python
SmartBuyGlasses中国:唯视良品(销售名牌太阳镜、墨镜和眼镜框)
2017/07/03 全球购物
印尼网上商店:Alfacart.com
2019/03/11 全球购物
期中考试后的反思
2014/02/08 职场文书
美容院店长岗位职责
2014/04/08 职场文书
服务承诺书格式
2014/05/21 职场文书
北京奥运会主题口号
2014/06/13 职场文书
服务行业演讲稿
2014/09/02 职场文书
公务员年度考核评语
2014/12/31 职场文书
公司内部升职自荐信
2015/03/27 职场文书
python实现大文本文件分割成多个小文件
2021/04/20 Python
缓存替换策略及应用(以Redis、InnoDB为例)
2021/07/25 Redis
Redis数据同步之redis shake的实现方法
2022/04/21 Redis