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;
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
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 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.