当前位置: 首页 > 图文教程 > 网络编程 > PHP > Search File Contents PHP 搜索目录文本内容的代码

PHP
discuz Passport 通行证 整合笔记
flash+php+mysql打造简单留言本教程
[原创]效率较高的php下读取文本文件的代码
phpmyadmin的安装与使用图文教程
IStream与TStream之间的相互转换
继续收藏一些PHP常用函数
php-5.2下php.ini 中文版配置说明
php单件模式结合命令链模式使用说明
PHPMailer邮件类利用smtp.163.com发送邮件方法
PHP编实现程动态图像的创建代码
php仿ZOL分页类代码
php仿discuz分页效果代码
攻克CakePHP(PHP中的Ruby On Rails框架)图文介绍
PHP ajax 分页类代码
PHP和Java 集成开发详解分析 强强联合
Linux下 php5 MySQL5 Apache2 phpMyAdmin ZendOptimizer安装与配置[图文]
IIS php环境配置PHP5 MySQL5 ZendOptimizer phpmyadmin安装与配置
Pear DB 新手入门指南教程
PHPMailer安装方法及简单实例
Apache+php+mysql在windows下的安装与配置图解(最新版)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 162 ::
收藏到网摘: n/a

这个类可以用来搜索在给定的文本目录中的文件。它可以给定目录遍历递归查找某些文件扩展名的文件。 这个类可以用来搜索在给定的文本目录中的文件。
它可以给定目录遍历递归查找某些文件扩展名的文件。
并打开找到的文件,并检查他们是否包含搜索词语。
它返回一个含有所有文件的列表包含搜索词语数组。
复制代码 代码如下:

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