This is a very useful program that can perform specific searches on text files and replace specified text with specific text. For example, if I have a typo in this article, there are dozens of them. It is very troublesome to find and modify them one by one. You can easily do it with the following method.--teaman.oso.com.cn
Class file search_replace.inc
class search_replace{ var $find; var $replace; var $files;
var $directories; var $include_subdir; var $ignore_lines;
var $ignore_sep;
var $occurences;
// Function definition and settings are as follows
', $include_subdir = 1, $ignore_lines = array()){
= $replace;
$ this->directories = $directories;
$this->include_subdir = $include_subdir;
$this->ignore_lines = $ignore_lines; ' search';
s(){
return $this->occurences; function get_last_error(){
//Set the FIND variable
function set_find($find) //Set the replace variable
replace;
. ;directories = $directories;
$this->include_subdir = $include_subdir;
.
$this->ignore_lines = $ignore_lines;
//确定是哪一种搜索方式
function set_search_function($search_function){
switch($search_function){
case 'normal': $this->search_function = 'search';
return TRUE;
break;
case 'quick' : $this->search_function = 'quick_search';
return TRUE;
break;
case 'preg' : $this->search_function = 'preg_search';
return TRUE;
break;
case 'ereg' : $this->search_function = 'ereg_search';
return TRUE;
break;
default : $this->last_error = 'Invalid search function specified';
return FALSE;
break;
}
}
//以下为搜索和替换程序的主文件
function search($filename){
$occurences = 0;
$file_array = file($filename);
for($i=0; $i
if(count($this->ignore_lines) > 0){
for($j=0; $j
if(substr($file_array[$i],0,strlen($this->ignore_lines[$j])) == $this->ignore_lines[$j]) $continue_flag = 1;
}
}
if($continue_flag == 1) continue;
$occurences += count(explode($this->find, $file_array[$i])) - 1;
$file_array[$i] = str_replace($this->find, $this->replace, $file_array[$i]);
}
if($occurences > 0) $return = array($occurences, implode('', $file_array)); else $return = FALSE;
return $return;
}
$file = fread($fp = fopen($filename, 'r'), filesize($ filename)); fclose($fp);
, $this-> replace, $file);
// preg search method does not support ignore_lines
function preg_search($filename){
$matches = preg_split($this->find, $file)) - 1;
if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return function ereg_search($filename){
$file = fread($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp); $file)) -1;
($occurences, $file ); else $return = FALSE;
//写新文件
function writeout($filename, $contents){
if($fp = @fopen($filename, 'w')){
fwrite($fp, $contents);
fclose($fp);
}else{
$this->last_error = 'Could not open file: '.$filename;
}
}
//由do_search调用,排出所有要搜索的文件
function do_files($ser_func){
if(!is_array($this->files)) $this->files = explode(',', $this->files);
for($i=0; $i
if($this->files[$i] == '.' OR $this->files[$i] == '..') continue;
if(is_dir($this->files[$i]) == TRUE) continue;
$newfile = $this->$ser_func($this->files[$i]);
if(is_array($newfile) == TRUE){
$this->writeout($this->files[$i], $newfile[1]);
$this->occurences += $newfile[0];
}
}
}
//由do_search()调用,排出所有要搜索的目录
function do_directories($ser_func){
if(!is_array($this->directories)) $this->directories = explode(',', $this->directories);
for($i=0; $i
$dh = opendir($this->directories[$i]);
while($file = readdir($dh)){
if($file == '.' OR $file == '..') continue;
if(is_dir($this->directories[$i].$file) == TRUE){
if($this->include_subdir == 1){
$this->directories[] = $this->directories[$i].$file.'/';
continue;
}else{
continue;
}
}
if(is_array($newfile) == TRUE){
->directories[$i].$file, $newfile[1]);
Call this do_search() You can start searching for files or directories
, _array($this->files) AND count($this-> ;files) > 0) OR $this->files != '') $this->do_files($this->search_function);
$this ->do_directories($this->search_function);
// End of class
?>
//The following is an example of calling this class, please save it as example.php
; /Establish new objects, set up search conditions, and finally return search results
$ SR = New Search_replace ('asp', 'php', array ('test.txt')); // Call the search and replace
$ sr- & gt; set_search_function('quick'); //Set search conditions
$sr->do_search();
$sr->set_find('another'); Customized return information
header('Content-Type: text/plain');
echo 'Discover and replace the following places: '.$sr->get_num_occurences()."rn"; echo 'Ah, error The following occurs.........: '.$sr->get_last_error()."rn";
?>
//Save the following text as test.txt, pay attention to text. txt must be readable and writable
"I like ASP very much. It is easy to learn and has powerful functions. I heard that ASP has accounted for most of the market. ASP is so good."
At this time, if you open exame.php, the following will appear These:
Discover and replace the following places: 3
Ah, the error occurred as follows...:
Check the test.txt file, and sure enough, the place where asp appears has been replaced by php.
The above introduces a good class for searching and replacing files or directories - it is very practical and includes various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.