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

PHP
PHP 变量类型的强制转换
PHP 判断变量类型实现代码
PHP 数组教程 定义数组
php后台程序与Javascript的两种交互方式
php 文件上传系统手记
php 网页游戏开发入门教程一(webgame+design)
PHP 简单日历实现代码
dedecms 批量提取第一张图片最为缩略图的代码(文章+软件)
php 显示指定路径下的图片
php pack与unpack 摸板字符字符含义
ThinkPHP php 框架学习笔记
PHP 批量删除数据的方法分析
PHP 读取和修改大文件的某行内容的代码
php实现jQuery扩展函数
浅谈PHP 闭包特性在实际应用中的问题
PHP 文件上传源码分析(RFC1867)
php 攻击方法之谈php+mysql注射语句构造
PHP+MySQL 手工注入语句大全 推荐
php UTF8 文件的签名问题
php 远程包含文件漏洞分析

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


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