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

PHP
PHP-Javascript“返回上一页”无缓存问题
新手入门:PHP编程中“数组”的基础知识
新手入门:PHP编程中“字符串”的小常识
PHP教程.经验技巧
php完美结合mysql数据库记录分页显示
php设计模式介绍之伪对象模式
PHP程序不实用大型系统的九大原因
PHP程序开发中的中文编码问题
PHP:避免重复提交和检查数据来路
动态网页技术PHP中错误处理的一些方法
单元测试在每个层上对PHP代码进行检查
学习动态网页制作PHP技术的正则表达式
PHP 5.0对象模型深度探索之起步
成为PHP高手 学会懒惰地用PHP编程
实例讲解基于DB2及PHP的应用系统跨平台迁移
如何书写安全的PHP代码
PHP教程:Ajax进行Web开发
Flash+PHP+Mysql简单留言本制作实例教程
理解PHP中的MVC框架编程
使用PHP与XML进行网站编程

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 189 ::
收藏到网摘: 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);
?>