這是個非常有用的程序,可以對文本文件進行特定的搜索,並以特定的文字替換指定的文字,舉個例子說如果我這篇文章裡有一個字全部打錯了,有幾十個地方,要一一找出來修改是件很麻煩的事,用下面這個就可以輕鬆搞定。--teaman.oso.com.cn
類別檔案search_replace.inc
class ;
var $ replace;
var $files;
var $include_subdir;
var $ignore_lines;
var $occurences;
var $search_function ;
var $last_error;
function search_replace($find, $replace, $files, $directories = '', $include_subdir = 1 , $ignore_lines = array()){
$this->replace $this->files = $files;
$this->directories $this->include_subdir = $include_subdir;
$this->occurences = 0;
$this->search_function = 'search'; error = '';
}
return $this->occurences; function get_last_error(){ turn $this->last_error;
}
//設定FIND變數
$this->find = $find;
) function set_replace($replace){
$this->replace = $replace;
}
//設置FILE變量
function set_files($files){
$this ->files = $files;
}
set_directories($directories){
🎜 >
//設定目錄變數set_include_subdir
$this->include_subdir = $include_subdir;
lines變數
function set_ignore_lines($ignore_lines){
}
//確定是哪一種搜尋方式
f switch($search_function){
';
返回TRUE;
休息;
case 'quick' : $this->search_function = 'quick_search';
返回TRUE;
休息;
case 'preg》 地 case 'ereg' : $this->search_function = 'ereg_search';
default : $this->last_error 返回FALSE; 的休息)
} unction search($filename){
$file_array = file($filename);
for $continue_flag = 0;
($this -this>ignore_lines) > 0){
j ){ strlen($this->ignore_lines[$j])) == $this->ignore_lines[$j]) $continue_flag = 1;
}
if($continue_flag == 1) continue ;
$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;
//使用quick(快速)搜索方法時,沒有igonre_lines功能
function quick_search($filename){
clearstatcache();
$file = fread ($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
$file if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
return $return;
}
//preg搜索方法不支持ignore_lines
function preg_search($filename){
clearstatcache ();
$file 🎜> $occurences = count($matches = preg_split($this->find, $file)) - 1;
if($occurences > 0) $return = array($occurences, $file); else $return = FALSE;
}
//ereg搜尋方法並未支援ignore_lines
function ereg_search($filename){
🎜>
$file = fread($fp = fopen($filename, 'r'), filesize($name), filesize($name) ; fclose($fp);
$ $file = ereg_replace($this->find, $this->replace, $file);
if($occurences > 0) $ return $return;
}
//寫新文件
if($fp = @fopen($filename, 'w')){
fwrite($fp, $contents);
}else{
🎜> }
}
// do_files($ser_func){
->files = explode(',', $this->files);
if($this -this>files[$i] == '.' OR $this->files[$i] == '..') continue;
== TRUE) continue;
if(is_array($newfile) = TRUEUE $this-> writeout($this->files[$i], $newfile[1]);
];
}
}
//由do_search()調用,導出所有要搜尋的目錄
if(!is_array($this->directories)) $this-& GT ;目錄=爆炸(',',$this->目錄);
for($ $dh = opendir( $this->directories[$i]);
if(is_dir($this->directories[$i].$file) == TRUE){
if($this->include_subdir == 1){
$this -> directories[] = $this->directories[$i].$file.'/';
續;
} else {
繼續;
}
-writeout($this->directories[$i].$file, $newfile[1]);
$this->occurences = $newfile[0];
}
}
}
//勾選 function do_search(){
if($this->find != '') {
if((is) s($this->search_function) ;
$this->do_directories($this->search_function); }
}// End of class
?>
//下面是呼叫該類別的例子說明,請存為example.php
'search_replace.inc'); //將文件包含進來
//建立新物件,設定搜尋條件、最後回傳搜尋結果
$sr = new search_replace('asp', ' $sr = new search_replace('asp', ' php', array('test.txt')); //呼叫搜尋與替換
$sr->set_search_function('quick'); //設定搜尋條件
🎜>
$sr->set_find('another');
$sr -Type : text/plain');
echo '發現並取代下列何者: '.$sr->get_num_occurences()."rn";
.......: '.$sr->get_last_error()."rn";
?>
//將以下文字存為test.txt,注意text.txt必須是可讀可寫的
"我非常喜歡asp,它簡單易學,功能強,聽說asp已經佔了大半市場,asp真好。"
此時,如果您打開exampe.php就會出現下面這些:
發現並替換以下幾個地方:3
啊,錯誤發生如下..........:
查看test.txt文件,果然出現asp的地方被php替換了。
http://www.bkjia.com/PHPjc/316312.html
www.bkjia.com