Search File Contents PHP 搜索目录文本内容的代码
时间:2023-11-24 08:09:40
这个类可以用来搜索在给定的文本目录中的文件。
它可以给定目录遍历递归查找某些文件扩展名的文件。
并打开找到的文件,并检查他们是否包含搜索词语。
它返回一个含有所有文件的列表包含搜索词语数组。
<?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,搜索目录,文本内容
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
页面中横排布局的思考
2008-01-18 12:56:00
![](https://img.aspxhome.com/file/UploadPic/20081/18/200811813731407s.jpg)
python 共现矩阵的实现代码
2021-12-22 14:42:33
![](https://img.aspxhome.com/file/2023/5/92435_0s.jpg)
django 环境变量配置过程详解
2021-11-19 03:07:52
![](https://img.aspxhome.com/file/2023/1/134381_0s.png)
python中pyplot直方图的绘制方式
2023-11-20 07:58:17
![](https://img.aspxhome.com/file/2023/1/63541_0s.png)
25个值得收藏的Python文本处理案例
2022-06-19 15:38:29
SQL 中主标识列IDENTITY使用技巧
2011-06-02 08:47:00
Go json反序列化“null“的问题解决
2024-02-18 22:54:19
![](https://img.aspxhome.com/file/2023/5/110515_0s.jpg)
Python中endswith()函数的基本使用
2022-06-05 04:01:34
Opera下cloneNode的bug
2007-11-23 11:40:00
基于Python实现帕累托图的示例详解
2022-06-28 05:48:00
![](https://img.aspxhome.com/file/2023/2/101562_0s.png)
python中pivot()函数基础知识点
2023-12-18 15:28:06
利用Python第三方库xlrd读取Excel中数据实例代码
2023-02-17 05:28:58
![](https://img.aspxhome.com/file/2023/6/121856_0s.jpg)
Python中创建二维数组
2023-11-24 15:14:54
![](https://img.aspxhome.com/file/2023/8/131508_0s.png)
python实现关闭第三方窗口的方法
2023-11-01 17:25:09
PHP基础用法讲解及phpinfo();演示
2023-05-29 08:34:29
![](https://img.aspxhome.com/file/2023/3/55373_0s.jpg)
vue项目中less的一些使用小技巧
2023-07-02 16:51:33
pytorch使用 to 进行类型转换方式
2022-10-05 23:28:27
![](https://img.aspxhome.com/file/2023/6/101576_0s.jpg)
Python二进制串转换为通用字符串的方法
2022-09-30 19:36:11
Keras搭建孪生神经网络Siamese network比较图片相似性
2023-01-27 04:16:28
![](https://img.aspxhome.com/file/2023/9/71999_0s.png)
js实现向右横向滑出的二级菜单效果
2024-04-17 10:34:44
![](https://img.aspxhome.com/file/2023/9/136229_0s.jpg)