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,搜索目录,文本内容
0
投稿

猜你喜欢

  • 页面中横排布局的思考

    2008-01-18 12:56:00
  • python 共现矩阵的实现代码

    2021-12-22 14:42:33
  • django 环境变量配置过程详解

    2021-11-19 03:07:52
  • python中pyplot直方图的绘制方式

    2023-11-20 07:58:17
  • 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
  • Python中endswith()函数的基本使用

    2022-06-05 04:01:34
  • Opera下cloneNode的bug

    2007-11-23 11:40:00
  • 基于Python实现帕累托图的示例详解

    2022-06-28 05:48:00
  • python中pivot()函数基础知识点

    2023-12-18 15:28:06
  • 利用Python第三方库xlrd读取Excel中数据实例代码

    2023-02-17 05:28:58
  • Python中创建二维数组

    2023-11-24 15:14:54
  • python实现关闭第三方窗口的方法

    2023-11-01 17:25:09
  • PHP基础用法讲解及phpinfo();演示

    2023-05-29 08:34:29
  • vue项目中less的一些使用小技巧

    2023-07-02 16:51:33
  • pytorch使用 to 进行类型转换方式

    2022-10-05 23:28:27
  • Python二进制串转换为通用字符串的方法

    2022-09-30 19:36:11
  • Keras搭建孪生神经网络Siamese network比较图片相似性

    2023-01-27 04:16:28
  • js实现向右横向滑出的二级菜单效果

    2024-04-17 10:34:44
  • asp之家 网络编程 m.aspxhome.com