Search File Contents PHP 搜索目录文本内容的代码

yipeiwu_com5年前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);
?>

相关文章

十个PHP高级应用技巧果断收藏

十个PHP高级应用技巧果断收藏

PHP 独特的语法混合了 C、Java、Perl 以及 PHP 自创新的语法。它可以比 CGI或者Perl更快速的执行动态网页。用PHP做出的动态页面与其他的编程语言相比,PHP是将程序...

php实现的debug log日志操作类实例

本文实例讲述了php实现的debug log日志操作类。分享给大家供大家参考,具体如下: <?php class Tool { public static func...

PHP使用Curl实现模拟登录及抓取数据功能示例

本文实例讲述了PHP使用Curl实现模拟登录及抓取数据功能。分享给大家供大家参考,具体如下: 使用PHP的Curl扩展库可以模拟实现登录,并抓取一些需要用户账号登录以后才能查看的数据。具...

php更新cookie内容的详细方法

cookie 是由服务器发送到浏览器的变量。cookie 通常是服务器嵌入到用户计算机中的小文本文件。 每当计算机通过浏览器请求一个页面,就会发送这个 cookie。 (推荐学习:PHP...

微信公众号开发之微信公共平台消息回复类实例

本文实例讲述了微信公众号开发之微信公共平台消息回复类。分享给大家供大家参考。具体如下: 微信公众号开发代码我在网上看到了有不少,其实都是大同小义了都是参考官方给出的demo文件进行修改的...