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);
?>


猜你喜欢
- 最近,我加入了Cloudera,在这之前,我在计算生物学/基因组学上已经工作了差不多10年。我的分析工作主要是利用Python语
- 本文实例为大家分享了python使用webdriver爬取微信公众号的具体代码,供大家参考,具体内容如下# -*- coding: utf-
- 如下代码可以计算输入的两张图像的结构相似度(SSIM),结果与matlab计算结果一致// An highlighted blockimpo
- 图片显示pytorch 载入的数据集是元组tuple 形式,里面包括了数据及标签(train_data,label),其中的train_da
- 前言Always On 可用性组活动辅助功能包括支持在辅助副本上执行备份操作。 备份操作可能会给 I/O 和 CPU 带来很大的压力(使用备
- MySQL 8 正式版 8.0.11 已发布,官方表示 MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能
- 说到这个话题,我们有个产品叫群组,为什么人们需要群组?简单说,群组就是个圈子,是有共同爱好和话题的人群聚在一起讨论、分享的地方。这个产品的诞
- //--------------------弹出层------------------- //popDivId:弹出层div的ID //dr
- 现在拥有了正则表达式这把神兵利器,我们就可以进行对爬取到的全部网页源代码进行筛选了。下面我们一起尝试一下爬取内涵段子网站:http://ww
- 本文实例为大家分享了python爬取淘宝商品的具体代码,供大家参考,具体内容如下import requests as req import
- 需要准备环境:python3.6、vultr(或者其他服务器)、xshell第一步:python安装必备环境Django库Xshell链接远
- 这里的Counter是指collections中的Counter,通过Counter可以实现字典的创建以及字典key出现频次的统计。然而,使
- 机器视觉从Google的无人驾驶汽车到可以识别假钞的自动售卖机,机器视觉一直都是一个应用广泛且具有深远的影响和雄伟的愿景的领域。这里我们将重
- 前言:在软件测试中,为项目编写接口自动化用例已成为测试人员常驻的测试工作。本文以python为例,基于笔者曾使用过的三种用例数据读取方法:x
- 在网上down了个web项目,在 IntelliJ IDEA 这个编辑器里面跑起来,但是发现domain文件夹下的xml文件都报如下的红色提
- canvas 粒子动画介绍何为canvascanvas是HTML5中新增的一个标签,主要是用于网页实时生成图像并可操作图像,它是用JavaS
- --PK select * from sys.key_constraints where object_id = OBJECT_ID(
- 如下所示:BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorE
- actions异步修改状态与mutations同步修改状态是两个容易混淆的概念,因为两者在执行上,很难测试出两者的差别,而我们要区别它们两,
- 如下所示:import sysfrom PyQt5.QtWidgets import *class MainWindow(QMainWind