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

这个类可以用来搜索在给定的文本目录中的文件。
它可以给定目录遍历递归查找某些文件扩展名的文件。
并打开找到的文件,并检查他们是否包含搜索词语。

它返回一个含有所有文件的列表包含搜索词语数组。

复制代码 代码如下:

<?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);
?>

时间: 2024-09-15 01:19:45

Search File Contents PHP 搜索目录文本内容的代码_php技巧的相关文章

PHP将HTML转换成文本的实现代码_php技巧

核心代码: <?php // $document 应包含一个 HTML 文档. // 本例将去掉 HTML 标记,javascript 代码 // 和空白字符.还会将一些通用的 // HTML 实体转换成相应的文本. $search = array ("'<script[^>]*?>.*?</script>'si", // 去掉 javascript "'<[\/\!]*?[^<>]*?>'si", //

PHP 写文本日志实现代码_php技巧

复制代码 代码如下: ** * 写文件 * @param string $file 文件路径 * @param string $str 写入内容 * @param char $mode 写入模式 */ function writeFile($file,$str,$mode='w') { $oldmask = @umask(0); $fp = @fopen($file,$mode); @flock($fp, 3); if(!$fp) { Return false; } else { @fwrite

PHP通过header实现文本文件下载的代码_php技巧

这就是今天讨论的主要问题.PHP帮助文档里面关于PHP通过header触发下载的说明比较简单,而网上关于此方面的文章也少的可怜,有很多文章都无法实现所需要的效果.今天我也来谈一下这个方面的话题,如果你感觉比网上的某些文章有所改进,那我就很知足了. 如果从准确的角度来说,那PHP文档是最准确的,因为它很简练的列出了实现文本类文件触发下载所需要的三条语句,以PDF为例就是: 复制代码 代码如下: // We'll be outputting a PDF header('Content-type: a

PHP 读取和修改大文件的某行内容的代码_php技巧

复制代码 代码如下: $fp = fopen('d:/file.txt', 'r+'); if ($fp) { $i = 1; while (!feof($fp)) { //修改第二行数据 if ($i == 2) { fseek($fp, 2, SEEK_CUR); fwrite($fp, '#'); break; } fgets($fp); $i++; } fclose($fp); } 这里需要注意的是fgets获取到一行后,文件指针指向行尾(也就是下一行开头),所以fwrite操作的是fg

php抓取https的内容的代码_php技巧

直接用file_get_contents,会报错: 复制代码 代码如下: $url = (https://xxx.com"); file_get_contents($url); 错误: Warning: file_get_contents(https://xxx.com) [function.file-get-contents]: failed to open stream: No such file or directory in D:wampwwwgrabber_clientindex.ph

php 全文搜索和替换的实现代码_php技巧

<?php  exec("/bin/grep -r '$oldword' $rootpath", $results, $errorCode);  if ($errorCode){  if ($errorCode == 1){  echo "Possibly no files were found with ?$oldword in them<BR>\n";  }  echo "OS Error: $errorCode<BR>\

php循环输出数据库内容的代码_php技巧

php do while方法     一般需要先$row=mysql_fetch_array($result)然后 do{something}while($row=mysql_fetch_array($result)) php while循环 while($row=mysql_fetch_array($result)){ }

按钮-C# 功能是清除选中的文本所有格式,不是文本内容,代码怎么写?

问题描述 C# 功能是清除选中的文本所有格式,不是文本内容,代码怎么写? 求大神指导,清除字体大小,颜色所有的格式,让他恢复原来的格式,代码是啥样的? 解决方案 ★"B.b"★★"B.b"★★"B.b"★ 解决方案二: 用richtextbox,获取选中的文本,clearallstyle,看看有没有类似的方法

php遍历替换目录下文件指定内容的方法_php技巧

本文实例讲述了php遍历替换目录下文件指定内容的方法.分享给大家供大家参考,具体如下: 在php中目录访问需要遍历了然后文件需要一个个打开进行访问操作了,下面我们来看一段php 替换目录下文件指定内容,具体如下: /**************************** * 获取目录下的所有文件 * [$dir] 文件夹路径 ****************************/ function deepScanDir($dir) { $fileArr = array (); $dirA