Search File Contents PHP 搜索目錄文本內(nèi)容的代碼
它可以給定目錄遍歷遞歸查找某些文件擴(kuò)展名的文件。
并打開(kāi)找到的文件,并檢查他們是否包含搜索詞語(yǔ)。
它返回一個(gè)含有所有文件的列表包含搜索詞語(yǔ)數(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
//開(kāi)源代碼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) ){ //開(kāi)源OSPhP.COM.CN
$contents = file_get_contents($f);
if ( strpos($contents, $this->search_phrase) !== false )
$this->foundFiles [] = $f;
//開(kāi)源代碼OSPhP.COm.CN
}
}
return $this->foundFiles;
}
function GetDirContents($dir){
if (!is_dir($dir)){die ("Function GetDirContents: Problem reading : $dir!");}
if ($root=@opendir($dir)){
//PHP開(kāi)源代碼
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; //開(kāi)源OSPhP.COM.CN
}
}
}
return $files;
}
}
//Example :
$search = new searchFileContents;
$search->search('E:/htdocs/AccessClass', 'class'); //開(kāi)源代碼OSPHP.COM.Cn
var_dump($search->foundFiles);
?>
相關(guān)文章
PHP JSON出錯(cuò):Cannot use object of type stdClass as array解決方法
這篇文章主要介紹了PHP JSON出錯(cuò):Cannot use object of type stdClass as array解決方法,需要的朋友可以參考下2014-08-08PHP實(shí)現(xiàn)的通過(guò)參數(shù)生成MYSQL語(yǔ)句類(lèi)完整實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)的通過(guò)參數(shù)生成MYSQL語(yǔ)句類(lèi),結(jié)合完整實(shí)例形式分析了生成MYSQL語(yǔ)句類(lèi)的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2016-04-04php沒(méi)有文件被上傳的實(shí)例分析及解決辦法
在本篇文章里小編給大家整理的是一篇關(guān)于php沒(méi)有文件被上傳的實(shí)例分析及解決辦法,有興趣的朋友們可以跟著學(xué)習(xí)參考下。2021-11-11使用PHP把HTML生成PDF文件的幾個(gè)開(kāi)源項(xiàng)目介紹
這篇文章主要介紹了使用PHP把HTML生成PDF文件的幾個(gè)開(kāi)源項(xiàng)目介紹,本文羅列了FPDF、DomPDF、TCPDF等項(xiàng)目的特點(diǎn),需要的朋友可以參考下2014-11-11PHP入門(mén)教程之?dāng)?shù)學(xué)運(yùn)算技巧總結(jié)
這篇文章主要介紹了PHP入門(mén)教程之?dāng)?shù)學(xué)運(yùn)算技巧,結(jié)合實(shí)例形式總結(jié)分析了php數(shù)值運(yùn)算、變量檢測(cè)、隨機(jī)數(shù)、絕對(duì)值、取整、最大值、最小值、四舍五入等操作技巧,需要的朋友可以參考下2016-09-09非常好用的兩個(gè)PHP函數(shù) serialize()和unserialize()
使用serialize()函數(shù)和unserialize()函數(shù),這兩個(gè)函數(shù)的用法真是絕配,一個(gè)是進(jìn)行序列化存儲(chǔ),另一個(gè)則是進(jìn)行序列化恢復(fù),方便極了2012-02-02php版微信公眾平臺(tái)回復(fù)中文出現(xiàn)亂碼問(wèn)題的解決方法
這篇文章主要介紹了php版微信公眾平臺(tái)回復(fù)中文出現(xiàn)亂碼問(wèn)題的解決方法,涉及php字符串編碼轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2016-09-09