Search File Contents PHP 搜索目錄文本內(nèi)容的代碼
更新時間:2010年02月21日 15:06:35 作者:
這個類可以用來搜索在給定的文本目錄中的文件。它可以給定目錄遍歷遞歸查找某些文件擴展名的文件。
這個類可以用來搜索在給定的文本目錄中的文件。
它可以給定目錄遍歷遞歸查找某些文件擴展名的文件。
并打開找到的文件,并檢查他們是否包含搜索詞語。
它返回一個含有所有文件的列表包含搜索詞語數(shù)組。
<?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);
?>
它可以給定目錄遍歷遞歸查找某些文件擴展名的文件。
并打開找到的文件,并檢查他們是否包含搜索詞語。
它返回一個含有所有文件的列表包含搜索詞語數(shù)組。
復制代碼 代碼如下:
<?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);
?>
相關(guān)文章
Fatal error: ''break'' not in the ''loop'' or ''switch'' con
PHPexcel報出錯誤Fatal error: 'break' not in the 'loop' or 'switch' context in Function.php on line 463.,需要的朋友可以參考下2021-06-06
php中比較簡單的導入phpmyadmin生成的sql文件的方法
做網(wǎng)站的時候 我們會制作一個安裝文件 就需要用到sql文件創(chuàng)建數(shù)據(jù)庫。分享一下 我所用的方法。2011-06-06
Linux平臺PHP5.4設(shè)置FPM線程數(shù)量的方法
這篇文章主要介紹了Linux平臺PHP5.4設(shè)置FPM線程數(shù)量的方法,較為詳細的分析了Linux平臺php5.4設(shè)置FPM的相關(guān)參數(shù)、功能及使用技巧,需要的朋友可以參考下2016-11-11
關(guān)于PHP模板Smarty的初級使用方法以及心得分享
本篇文章是對PHP模板Smarty的初級使用方法以及心得進行了詳細的分析介紹,需要的朋友參考下2013-06-06

