首頁 > 後端開發 > php教程 > 搜尋和取代檔案或目錄的好類--很實用

搜尋和取代檔案或目錄的好類--很實用

WBOY
發布: 2016-07-29 08:34:19
原創
843 人瀏覽過

這是個非常有用的程序,可以對文本文件進行特定的搜索,並以特定的文字替換指定的文字,舉個例子說如果我這篇文章裡有一個字全部打錯了,有幾十個地方,要一一找出來修改是件很麻煩的事,用下面這個就可以輕鬆搞定。--teaman.oso.com.cn
類別檔案search_replace.inc
        class search_replace{
              var $replace;
                var $files;
                var    var $ignore_lines;
                var $ignore_sep;
              var $search_function;
                var $last_error;
        //以下進行函數定義與設定
                function search_replace($find, $replace, $11, $. {
                        $this- >find            = $find;
                                        $this->files                $this->directories     = $directories;
               = $include_subdir;
                        $this->          $this->occurences      = 0;
                                  $this->last_error      = ' ';
                }
        /**                        return $this->occurences;                 function get_last_error(){
                         }
        //設定FIND變數
                ,       $this->find = $find;
                }                         $this->replace = $replace;                 function set_files($files){    = $files;
                }
        //設定目錄變數
                            $this->directories = $directories;                 function set_include_subdir ($include_subdir){
                          }
        //設定ignore_lines變數
                         $this->ignore_lines = $ignore_lines ;
                }
        //確定是哪一種搜尋方式
                         switch($search_function){
                   🎜>                                                返回TRUE;
                                                休息;
                                case 'quick' : $this->search_function = 'quick_search';
                                                返回TRUE;
                                                休息;
                                case 'preg'  : $this- >search_function = 'preg_search';
                                                返回TRUE;
                                                休息;
                                case 'ereg'  : $this->search_function = 'ereg_search';
                                                返回TRUE;
                                                休息;
default      : $this->last_error      = '指定的搜尋功能無效';
                        返回FALSE;🎜>                     休息;
                        }
                }
        //以下為搜索和替換程序的主文件
                function search($filename){
                        $occurrences = 0;
                        $file_array = file($filename);
                        for($i=0; $i                  >                                     ($j=0; $jignore_lines); $j++){
                      如果(substr($file_array[$i],0,strlen($this->ignore_lines[$j] ) == $this->ignore_lines[$j]) $continue_flag = 1;
                                                }
       if($continue_flag == 1) 續;
                        explode($this->find, $file_array[$i])) - 1;
                     替換, $file_array[$i]) ;
                        }
      turn = array($occurrences, implode('', $file_array));否則$return = FALSE;
                     }
        //使用quick(快速)搜尋方法時,無igonre_lines功能
                           clearstatcache();
                'r'), filesize($filename)); fclose($fp);
                                         $file       = str_replace($ this->find, $this->replace, $file);
                        if($occurencesturn > 0) $.$.                      return $return;
                }
        //preg ){
                        clearstatcache();          = fread($fp = fopen($filename, 'r '), filesize($filename)); fclose($fp);
                                            $file       = preg_replace( $this->find, $this->replace, $file);
                        if($occurencesturn > 0) $.$.                      return $return ;
                }
        //ereg  filename){
                        clearstatcache();   read($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp);
                                          $file       = ereg_replace($this->find, $this->replace, $file);
                       🎜>                        return $return;
                }
        //寫入新檔案
                      if($fp = @fopen($filename, 'w')){
                  $內容);
                                fclose($fp);
                        }else{
         $this->last_error = '無法開啟檔案: '.$filename;
                        }
       ,提交所有要搜尋的文件
               function do_files($ser_func){
files)) $this ->檔= 爆炸(',', $this->檔);
                        for($i=0; $ifiles)             if($this->files[$i] == '.' OR $this->文件[$i] == '..') 繼續;
                                if(is_dir($this->files[UEUE]) ==
                               
                                                        $this->writeout($this->files[$i], $newfile[1 ]);
                                  
                                  }
                }
        //由do_search()調用,導出所有要搜尋的目錄
                             if(!is_array($this->directories)) $this->directories = explode( ',', $this->目錄);
                        for($i=00 $                     $dh = opendir($this->directories [$i]);
                                                         if($file == '.' OR $file                     if( is_dir($this->directories[$i].$file) == TRUE){     if($this->include_subdir == 1){
                                $this->目錄[] = $this->目錄[$ i].$file.'/';
                                                        繼續;
} else {
繼續;
                                                }
                                         }
                                                                                                       地($this->directories[$i].$file, $newfile[1]);
                        $this->occurences += $newfile[0];
                                                   }
      🎜>                }
        //呼叫這個do_search() search(){
                        if($this->find !=             if ((is_array($this->files) A​​ND count($this->files) > 0) OR $this->files != '') $this->do_files($this->search_function); directories != '')                                                    }
           // End of class
?>
//下面是呼叫該類別的例子說明,請存為example.php
        include('search_replace.inc');將檔案包含進來
//建立新物件,設定搜尋條件、最後回傳搜尋結果
        $sr = new search_replace('asp', 'php', array('test.txt')); //呼叫搜尋與替換
        $sr->set_search_function('quick');   //設定搜尋條件
       );
        $sr->do_search();
//下面是客製化的回傳資訊
        header('Content-Type: .$sr->get_num_occurences()."rn";
        echo '啊,錯誤發生如下.............: '.$sr->get_last_error()."rn" ;
?>
//將以下文字存為test.txt,注意text.txt必須是可讀可寫的
"我很喜歡asp,它簡單易學,功能強,聽說asp已經佔了大半市場,asp真好。 ................:      
查看test.txt文件,果然出現asp的地方被php取代了。

以上就介紹了 搜尋和替換文件或目錄的一個好類--很實用,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。


相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板