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模拟SQL Server的两个日期处理函数
Oct 09 PHP
php visitFile()遍历指定文件夹函数
Aug 21 PHP
PHP实现提取一个图像文件并在浏览器上显示的代码
Oct 06 PHP
Zend Studio 实用快捷键一览表(精心整理)
Aug 10 PHP
php时间戳转换的示例
Mar 31 PHP
PHP+Mysql+Ajax+JS实现省市区三级联动
May 23 PHP
php+html5基于websocket实现聊天室的方法
Jul 17 PHP
PHP编写的图片验证码类文件分享
Jun 06 PHP
PHP Ajax实现无刷新附件上传
Aug 17 PHP
详谈配置phpstorm完美支持Codeigniter(CI)代码自动完成(代码提示)
Apr 07 PHP
Yii2框架中一些折磨人的坑
Dec 15 PHP
php提高脚本性能的4个技巧
Aug 18 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
Memcached常用命令以及使用说明详解
2013/06/27 PHP
php后门URL的防范
2013/11/12 PHP
php递归方法实现无限分类实例代码
2014/02/28 PHP
PHP入门经历和学习过程分享
2014/04/11 PHP
php注册系统和使用Xajax即时验证用户名是否被占用
2017/08/31 PHP
Autocomplete Textbox Example javascript实现自动完成成功
2007/08/17 Javascript
JQuery中的ready函数冲突的解决方法
2010/05/17 Javascript
js Html结构转字符串形式显示代码
2011/11/15 Javascript
ie下$.getJSON出现问题的解决方法
2014/02/12 Javascript
zeroclipboard 单个复制按钮和多个复制按钮的实现方法
2014/06/14 Javascript
Eclipse配置Javascript开发环境图文教程
2015/01/29 Javascript
JavaScript动态修改网页元素内容的方法
2015/03/21 Javascript
javascript常用经典算法详解
2017/01/11 Javascript
在vue-cli项目中使用bootstrap的方法示例
2018/04/21 Javascript
vue-router中scrollBehavior的巧妙用法
2018/07/09 Javascript
微信小程序实现顶部下拉菜单栏
2018/11/04 Javascript
Vue ElementUI实现:限制输入框只能输入正整数的问题
2020/07/31 Javascript
WebStorm无法正确识别Vue3组合式API的解决方案
2021/02/18 Vue.js
Python查询阿里巴巴关键字排名的方法
2015/07/08 Python
详解python中字典的循环遍历的两种方式
2017/02/07 Python
Python随机生成均匀分布在三角形内或者任意多边形内的点
2017/12/14 Python
Python中实现switch功能实例解析
2018/01/11 Python
python实现简易动态时钟
2018/11/19 Python
用Python制作音乐海报
2021/01/26 Python
CSS3实现多重边框的方法总结
2016/05/31 HTML / CSS
美国在线鞋类零售商:LifeStride
2019/06/09 全球购物
为您的家、后院、车库等在线购物:Spreetail
2019/06/17 全球购物
高等学院职业生涯规划书范文
2014/09/16 职场文书
北京离婚协议书范文2014
2014/09/29 职场文书
镇党政领导班子民主生活会思想汇报
2014/10/11 职场文书
2014年反腐倡廉工作总结
2014/12/05 职场文书
2015年检验员工作总结范文
2015/04/30 职场文书
辩护词格式
2015/05/22 职场文书
实战 快速定位MySQL的慢SQL
2022/03/22 MySQL
Lakehouse数据湖并发控制陷阱分析
2022/03/31 Oracle
一文搞懂Redis中String数据类型
2022/04/03 Redis