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 相关文章推荐
我的论坛源代码(八)
Oct 09 PHP
Apache2 httpd.conf 中文版
Dec 06 PHP
adodb与adodb_lite之比较
Dec 31 PHP
PHP扩展编写点滴 技巧收集
Mar 09 PHP
win7下memCache的安装过程(具体操作步骤)
Jun 28 PHP
PHP基本语法总结
Sep 06 PHP
PHP编译安装时常见错误解决办法
May 28 PHP
php实现将Session写入数据库
Jul 26 PHP
PHP错误处理函数
Apr 03 PHP
thinkphp5 migrate数据库迁移工具
Feb 20 PHP
PHP操作Redis常用技巧总结
Apr 24 PHP
PHP中国际化的字符串排序和比较对象详解
Aug 23 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
神族 Protoss 历史背景
2020/03/14 星际争霸
使用Apache的htaccess防止图片被盗链的解决方法
2013/04/27 PHP
php实现发送微信模板消息的方法
2015/03/07 PHP
php文件下载处理方法分析
2015/04/22 PHP
php获取图片信息的方法详解
2015/12/10 PHP
php array_chunk()函数用法与注意事项
2019/07/12 PHP
使用SyntaxHighlighter实现HTML高亮显示代码的方法
2010/02/04 Javascript
javascript中encodeURI和decodeURI方法使用介绍
2013/05/06 Javascript
JS仿百度搜索自动提示框匹配查询功能
2013/11/21 Javascript
jquery 提示信息显示后自动消失的具体实现
2013/12/18 Javascript
js中判断对象是否为空的三种实现方法
2013/12/23 Javascript
jQuery自定义添加&quot;$&quot;与解决&quot;$&quot;冲突的方法
2015/01/19 Javascript
浅析JavaScript声明变量
2015/12/21 Javascript
深入理解Node.js中的进程管理
2017/03/13 Javascript
自制简易打赏功能的实例
2017/09/02 Javascript
快速处理vue渲染前的显示问题
2018/03/05 Javascript
vue中el-upload上传图片到七牛的示例代码
2018/10/19 Javascript
vue实现路由切换改变title功能
2019/05/28 Javascript
vue使用@scroll监听滚动事件时,@scroll无效问题的解决方法详解
2019/10/15 Javascript
vue内置组件component--通过is属性动态渲染组件操作
2020/07/28 Javascript
搭建vscode+vue环境的详细教程
2020/08/31 Javascript
vue-simple-uploader上传成功之后的response获取代码
2020/09/07 Javascript
Python、Javascript中的闭包比较
2015/02/04 Python
[原创]使用豆瓣提供的国内pypi源
2017/07/02 Python
Python实现繁?转为简体的方法示例
2018/12/18 Python
PyQt打开保存对话框的方法和使用详解
2019/02/27 Python
python实现接口并发测试脚本
2019/06/25 Python
Django为窗体加上防机器人的验证码功能过程解析
2019/08/14 Python
python遍历文件目录、批量处理同类文件
2019/08/31 Python
解决pip install psycopg2出错问题
2020/07/09 Python
Sarenza德国:法国最大的时尚鞋和包包网上商店
2019/06/08 全球购物
Flesh Beauty官网:露华浓集团旗下彩妆品牌
2021/02/15 全球购物
全球性的众包图形设计市场:DesignCrowd
2021/02/02 全球购物
拾金不昧感谢信
2015/01/21 职场文书
工厂采购员岗位职责
2015/04/07 职场文书
创业计划之特色精品店
2019/08/12 职场文书