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 in_array 函数使用说明与in_array需要注意的地方说明
Apr 13 PHP
php在多维数组中根据键名快速查询其父键以及父键值的代码
May 07 PHP
QQ登录 PHP OAuth示例代码
Jul 20 PHP
win2003服务器使用WPS的COM组件的一些问题解决方法
Jan 11 PHP
浅析PHP页面局部刷新功能的实现小结
Jun 21 PHP
php从给定url获取文件扩展名的方法
Mar 14 PHP
PHP中的事务使用实例
May 26 PHP
讲解WordPress开发中一些常用的debug技巧
Dec 18 PHP
CodeIgniter配置之routes.php用法实例分析
Jan 19 PHP
php实现登陆模块功能示例
Oct 20 PHP
php图片合成方法(多张图片合成一张)
Nov 25 PHP
PHP模版引擎原理、定义与用法实例
Mar 29 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
收音机玩机评测 406 篇视频合集
2020/03/11 无线电
php基础知识:函数基础知识
2006/12/13 PHP
同台服务器使用缓存APC效率高于Memcached的演示代码
2010/02/16 PHP
PHP往XML中添加节点的方法
2015/03/12 PHP
PHP、Java des加密解密实例
2015/04/27 PHP
大家在抢红包,程序员在研究红包算法
2015/08/31 PHP
php使用flock阻塞写入文件和非阻塞写入文件的实例讲解
2017/07/10 PHP
Nginx实现反向代理
2017/09/20 Servers
php对微信支付回调处理的方法
2018/08/23 PHP
常见的5个PHP编码小陋习以及优化实例讲解
2021/02/27 PHP
js字符编码函数区别分析
2008/06/05 Javascript
图片无缝滚动代码(向左/向下/向上)
2013/04/10 Javascript
BootStrop前端框架入门教程详解
2016/12/25 Javascript
jQuery实现最简单实用的分秒倒计时
2017/02/05 Javascript
js实现登录框鼠标拖拽效果
2017/03/09 Javascript
详解如何将angular-ui的图片轮播组件封装成一个指令
2017/05/09 Javascript
浅谈js中的this问题
2017/08/31 Javascript
详解Jest结合Vue-test-utils使用的初步实践
2019/06/27 Javascript
js屏蔽F12审查元素,禁止修改页面代码等实现代码
2020/10/02 Javascript
原生JavaScript实现轮播图
2021/01/10 Javascript
Python虚拟环境Virtualenv使用教程
2015/05/18 Python
python中__slots__用法实例
2015/06/04 Python
python 判断三个数字中的最大值实例代码
2019/07/24 Python
python字符串格式化方式解析
2019/10/19 Python
详解python itertools功能
2020/02/07 Python
用canvas做一个DVD待机动画的实现代码
2019/04/12 HTML / CSS
AmazeUI的JS表单验证框架实战示例分享
2020/08/21 HTML / CSS
猎人靴英国官网:Hunter Boots
2017/02/02 全球购物
机电工程学生自荐信范文
2013/12/07 职场文书
父亲追悼会答谢词
2014/01/17 职场文书
2014年综治宣传月活动总结
2014/04/28 职场文书
大学新生入学教育方案
2014/05/16 职场文书
工资收入证明
2014/10/07 职场文书
后勤个人工作总结
2015/02/28 职场文书
2015年工程部工作总结
2015/04/30 职场文书
HTML中实现音乐或视频自动播放案例详解
2022/05/30 HTML / CSS