A good class for searching and replacing files or directories--very useful

Source: Internet
Author: User
Tags ereg fread preg
This is a very useful program, you can make a specific search for text files, and replace the specified text with a specific text, for example, if I have a word in this article all wrong, there are dozens of, to find out to modify is a very troublesome thing, with the following this can be easily done. --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;
var $search _function;
var $last _error;
The following functions are defined and set
function Search_replace ($find, $replace, $files, $directories = ", $include _subdir = 1, $ignore _lines = Array ()) {
$this->find = $find;
$this->replace = $replace;
$this->files = $files;
$this->directories = $directories;
$this->include_subdir = $include _subdir;
$this->ignore_lines = $ignore _lines;
$this->occurences = 0;
$this->search_function = ' search ';
$this->last_error = ";
}
/***************************************
* * Accessor for retrieving occurences.
***************************************/
function Get_num_occurences () {
return $this->occurences;
}
Get the last Error
function Get_last_error () {
return $this->last_error;
}
Set the Find variable
function Set_find ($find) {
$this->find = $find;
}
Set the Replace variable
function Set_replace ($replace) {
$this->replace = $replace;
}
Setting the file variable
function Set_files ($files) {
$this->files = $files;
}
Set Directory variables
function Set_directories ($directories) {
$this->directories = $directories;
}
Set directory variable Set_include_subdir
function Set_include_subdir ($include _subdir) {
$this->include_subdir = $include _subdir;
}
Setting the Ignore_lines variable
function Set_ignore_lines ($ignore _lines) {
$this->ignore_lines = $ignore _lines;
}
Determine which search method is
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
}
}
The following is the main file for the search and replace program
function Search ($filename) {
$occurences = 0;
$file _array = file ($filename);
for ($i =0; $i $continue _flag = 0;
if (count ($this->ignore_lines) > 0) {
for ($j =0; $j Ignore_lines); $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;
}
No Igonre_lines function When using Quick Search method
function Quick_search ($filename) {
Clearstatcache ();
$file = fread ($fp = fopen ($filename, ' R '), FileSize ($filename)); Fclose ($FP);
$occurences = count (Explode ($this->find, $file))-1;
$file = Str_replace ($this->find, $this->replace, $file);
if ($occurences > 0) $return = Array ($occurences, $file); else $return = FALSE;
return $return;
}
Preg Search method does not support Ignore_lines
function Preg_search ($filename) {
Clearstatcache ();
$file = fread ($fp = fopen ($filename, ' R '), FileSize ($filename)); Fclose ($FP);
$occurences = count ($matches = Preg_split ($this->find, $file))-1;
$file = Preg_replace ($this->find, $this->replace, $file);
if ($occurences > 0) $return = Array ($occurences, $file); else $return = FALSE;
return $return;
}
Ereg Search method also does not support Ignore_lines
function Ereg_search ($filename) {
Clearstatcache ();
$file = fread ($fp = fopen ($filename, ' R '), FileSize ($filename)); Fclose ($FP);
$occurences = count ($matches = Split ($this->find, $file))-1;
$file = ereg_replace ($this->find, $this->replace, $file);
if ($occurences > 0) $return = Array ($occurences, $file); else $return = FALSE;
return $return;
}
Write a new file
function Writeout ($filename, $contents) {
if ($fp = @fopen ($filename, ' W ')) {
Fwrite ($fp, $contents);
Fclose ($FP);
}else{
$this->last_error = ' Could not open file: '. $filename;
}
}
Called by Do_search to drain all files to be searched.
function Do_files ($ser _func) {
if (!is_array ($this->files)) $this->files = Explode (', ', $this->files);
for ($i =0; $i files); $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];
}
}
}
Called by Do_search () to drain all directories to be searched
function do_directories ($ser _func) {
if (!is_array ($this->directories)) $this->directories = Explode (', ', $this->directories);
for ($i =0; $i directories); $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
}
}
$newfile = $this $ser _func ($this->directories[$i]. $file);
if (Is_array ($newfile) = = TRUE) {
$this->writeout ($this->directories[$i]. $file, $newfile [1]);
$this->occurences + = $newfile [0];
}
}
}
}
Call this do_search () to begin searching for a file or directory
function Do_search () {
if ($this->find! = ") {
if (Is_array ($this->files) and Count ($this->files) > 0) OR $this->files! = ") $this->do_files ($this- >search_function);
if ($this->directories! = ") $this->do_directories ($this->search_function);
}
}
}//End of Class
?>
Here is an example of calling this class, save as Example.php
Include (' Search_replace.inc '); Include the file in
Create new objects, set search criteria, and finally return search results
$SR = new Search_replace (' asp ', ' php ', Array (' test.txt ')); Call Search and replace
$SR->set_search_function (' quick '); Set search criteria
$SR->do_search ();
$SR->set_find (' another ');
$SR->do_search ();
The following is a custom return message
Header (' Content-type:text/plain ');
Echo ' finds and replaces the following places: '. $SR->get_num_occurences (). " \ r \ n ";
Echo ' Ah, the error has occurred as follows ..... $SR->get_last_error (). \ r \ n ";
?>
Save the following text as Test.txt, note that text.txt must be readable and writable
"I like ASP, it is simple and easy to learn, strong function, I heard that ASP has accounted for the majority of the market, ASP is really good." "
At this point, if you open exampe.php, the following will appear:
Discover and replace the following places: 3
Ah, the error occurs as follows .....:
View the Test.txt file, and sure enough, the place where the ASP occurred was replaced by PHP.

The above is a good way to search and replace files or directories-very useful, including aspects of the content, I hope to be interested in the PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.