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

PHP
php 正确解码javascript中通过escape编码后的字符
在PHP中养成7个面向对象的好习惯
PHP函数常用用法小结
Zend framework处理一个http请求的流程分析
用js进行url编码后用php反解以及用php实现js的escape功能函数总结
php cli 方式 在crotab中运行解决
PHPWind 发帖回帖Api PHP版打包下载
Linux下将excel数据导入到mssql数据库中的方法
php不用内置函数对数组排序的两个算法代码
用php实现的下载css文件中的图片的代码
php 获取当前访问的url文件名的方法小结
php date与gmdate的获取日期的区别
php下把数组保存为文件格式的实例应用
劣质的PHP代码简化
php 生成随机验证码图片代码
php+mysql事务rollback&commit示例
php 处理上百万条的数据库如何提高处理查询速度
两个开源的Php输出Excel文件类
PHP Memcached应用实现代码
Memcache 在PHP中的使用技巧

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


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