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调用三种数据库的方法(1)
Oct 09 PHP
解决phpmyadmin中文乱码问题。。。
Jan 18 PHP
在Windows系统上安装PHP运行环境文字教程
Jul 19 PHP
关于Iframe如何跨域访问Cookie和Session的解决方法
Apr 15 PHP
PHP以指定字段为索引返回数据库所取的数据数组
Jun 30 PHP
php读取csv文件后,uft8 bom导致在页面上显示出现问题的解决方法
Aug 10 PHP
PHP中上传多个文件的表单设计例子
Nov 19 PHP
php使用Jpgraph绘制复杂X-Y坐标图的方法
Jun 10 PHP
学习php设计模式 php实现访问者模式(Visitor)
Dec 07 PHP
thinkphp3.2.3 分页代码分享
Jul 28 PHP
解决laravel中日志权限莫名变成了root的问题
Oct 17 PHP
基于php伪静态的实现方法解析
Jul 31 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中printf()函数格式化使用
2016/05/23 PHP
浅谈PHP表单提交(POST&amp;GET&amp;URL编/解码)
2017/04/03 PHP
使用laravel和ajax实现整个页面无刷新的操作方法
2019/10/03 PHP
一组JS创建和操作表格的函数集合
2009/05/07 Javascript
js jquery数组介绍
2012/07/15 Javascript
基于jQuery中对数组进行操作的方法
2013/04/16 Javascript
jQuery中对节点进行操作的相关介绍
2013/04/16 Javascript
JS远程获取网页源代码实例
2013/09/05 Javascript
eclipse如何忽略js文件报错(附图)
2013/10/30 Javascript
基于jquery的simpleValidate简易验证插件
2014/01/31 Javascript
javascript结合canvas实现图片旋转效果
2015/05/03 Javascript
jQuery实现向下滑出的二级菜单效果实例
2015/08/22 Javascript
BootStrap中的table实现数据填充与分页应用小结
2016/05/26 Javascript
Angular在一个页面中使用两个ng-app的方法
2017/02/20 Javascript
jquery动态赋值id与动态取id方法示例
2017/08/21 jQuery
微信小程序实现图片上传、删除和预览功能的方法
2017/12/18 Javascript
python判断端口是否打开的实现代码
2013/02/10 Python
用Python计算三角函数之atan()方法的使用
2015/05/15 Python
Python爬虫DNS解析缓存方法实例分析
2017/06/02 Python
django的ORM模型的实现原理
2019/03/04 Python
python画微信表情符的实例代码
2019/10/09 Python
python socket 聊天室实例代码详解
2019/11/14 Python
python科学计算之narray对象用法
2019/11/25 Python
Spring实战之使用util:命名空间简化配置操作示例
2019/12/09 Python
python关于倒排列的知识点总结
2020/10/13 Python
python 爬虫之selenium可视化爬虫的实现
2020/12/04 Python
python3中for循环踩过的坑记录
2020/12/14 Python
La Senza官网:北美顶尖性感内衣品牌
2018/08/03 全球购物
Hotels.com拉丁美洲:从豪华酒店到经济型酒店的预定优惠和折扣
2019/12/09 全球购物
一套带网友答案的.NET笔试题
2016/12/06 面试题
幼儿园推普周活动总结
2015/05/07 职场文书
加班费申请报告
2015/05/15 职场文书
结婚喜宴迎宾词
2015/08/10 职场文书
创业计划书之校园超市
2019/09/12 职场文书
使用Redis做预定库存缓存功能
2022/04/02 Redis
Python matplotlib安装以及实现简单曲线的绘制
2022/04/26 Python