Search File Contents PHP 搜索目录文本内容的代码
发布时间:2023-11-24 08:09:40
标签:PHP,搜索目录,文本内容
这个类可以用来搜索在给定的文本目录中的文件。
它可以给定目录遍历递归查找某些文件扩展名的文件。
并打开找到的文件,并检查他们是否包含搜索词语。
它返回一个含有所有文件的列表包含搜索词语数组。
<?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);
?>
0
投稿
猜你喜欢
- 如图:Oracle 11g安装到42%挂了。上度娘查了一下,原来是Oracle安装包的问题,1,2两个包都要下载下来,而且需要解压到相同(同
- 或许现在关心交互设计的设计师们大部分来自于了互联网行业,所以我们看到当你搜索“交互设计”时更多的BLOG和文章是在谈论互联网,网站的导航,注
- --------------------------------------------------------- 正则收藏 手机号码: $
- 一、为表创建自增长自段有两种,一种是不同的表使用各自的Sequence,方法如下: 1、在Oracle sequence首先创建sequen
- 列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式。一个循环在C语言等其他
- 如下所示:'''Created on 2018-4-20例子:每天凌晨3点执行func方法''
- 代码如下: <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%&
- 效果图展示:源码查看【功能说明】利用insertBefore制作简单的循环插空效果【HTML代码说明】<ul class="
- 如果是在Oracle10g之前,删除一个表空间中的数据文件后,其文件在数据库数据字典中会仍然存在,除非你删除表空间,否则文件信息不会清除。但
- 问题:如果一个网站拥有两个域名:domain1.com和domain2.com。在网站运营前期,主推domain1.com,但发展到中期,由
- 最近看到N多介绍CSS框架,前些天我说过一句话:“在我有限的视野里,还没见到可以真正可以称得上css框架的东东~”,当然也可能是我的视野太小
- 【原文地址】Tip/Trick: Url Rewriting with ASP.NET 【原文发表日期】 Monday, February
- 在编程时你一定碰到过时间触发的事件,在VB中有timer控件,而asp中没有,假如你要不停地查询数据库来等待一个返回结果的话,我想你一定知道
- header() 函数向客户端发送原始的 HTTP 报头。<?php//200 正常状态header('HTTP/1.1 20
- 全球数据量的疯狂增长,使得市场对资深数据库管理员的需求也节节攀升。据统计,一直到2016美国IT市场对数据库管理员的需求量增长都将会超过所有
- 一、 了解postman1. 什么是postman?------ 软件测试用来做接口测试的工具。2. 如何下载postman--
- 近段时间由于修改一个ASP程序(有SQL注入漏洞),在网上找了很多相关的一些防范办法,都不近人意,所以我将现在网上的一些方法综合改良了一下,
- 这是一篇知识性的文档,主要目的是为了让Web缓存相关概念更容易被开发者理解并应用于实际的应用环境中。为了简要起见,某些实现方面的细节被简化或
- CSS Sprites技术不新鲜,早在2005年 CSS Zengarden 的园主 Dave Shea 就在 ALA
- 项目有时要用一些Ajax的效果,因为比较简单,也就没有去用什么Ajax.net之类的东西,手写代码也就实现了。、第二天,有人反馈错误;说是只