Home  >  Article  >  Backend Development  >  A good class for searching and replacing files or directories--very practical_PHP tutorial

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

WBOY
WBOYOriginal
2016-07-21 16:03:16799browse

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 $replace;
var $files;
var $directories;
var $include_subdir;
var $ignore_lines; > var $ignore_sep;
var $occurences;
var $
search_function; 1, $ignore_lines = array()){

$this->find = $find;
$this->replace = $replace;
                                                                                                   ​ files;
$this->directories = $directories; $this->include_subdir = $inc_subdir;lude $this->ignore_lines = $ignore_lines;

$this- >occurences = 0;
$this->search_function = 'search';
last_error = '';

                                                                                                                                            🎝>*/
function get_num_occurences(){
                                                                         //Get the last error
                  function get_last_error(){
return $this->last_error;
                              

                                                                                                                                                                                        🎜>
//Set the replace variable
              function set_replace($replace){
                                                                                                               FILE variable
function set_files($files){
$this->files = $files;
                                                                                               $this->directories = $directories;
                                                                                                                        this->include_subdir = $include_subdir;
                                                                                                           ignore_lines variable
function set_ignore_lines($ignore_lines){
$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                                $continue_flag = 0;
                                if(count($this->ignore_lines) > 0){
                                        for($j=0; $jignore_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;

                }

//When using the quick search method, there is no igonre_lines function
function quick_search($filename){

clearstatcache();

$file = fread ($fp = fopen($filename, 'r'), filesize($filename)); fclose($fp); $ File = Str_replace; CES, $ file); else $return = FALSE;
                                                        return $return;                                                                      function preg_search($filename){

                                                                                                                   🎜> $occurences = count($matches = preg_split($this->find, $file)) - 1;
                                                               
                                                                                                                                                                                                                                                                The /ereg search method does not support ignore_lines
function ereg_search($filename){

clearstatcache(); $file = fread($fp = fopen($filename, 'r' ), filesize($filename)); fclose($fp); = ereg_replace($this->find, $this->replace, $file);

                                                                              ​= FALSE;
                             return $return;                               

                                                                                                                  fwrite($fp, $contents);
                                                                 $this->last_error = 'Could not open file: '.$filename;                                                        🎜>
                                                                                                If(!is_array($this->files)) $this->files = explode(',', $this->files);
for($i=0; $ifiles); $i++){
                                                                                                                                                                                                                 if ($this->files[$i] == '.' OR $this->files[$i] == '..') continue; -> files[$i]) == TRUE) continue;
                                                          is_array($newfile) == TRUE){
                                                                                 $this->occurences += $newfile[0];
                                                                                                                                                                                                ​

        //由do_search()调用,排出所有要搜索的目录
                function do_directories($ser_func){
                        if(!is_array($this->directories)) $this->directories = explode(',', $this->directories);
                        for($i=0; $idirectories); $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){
$this->writeout($this->directories[$i].$file, $newfile[1]);
                                                                                                                                                              }                                      🎜>} }
}

// Call this do_search () to start searching the file or directory
Function Do_search () {
If ($ This- & GT ;find != ''){
                                                                                                                                                                           this->do_files($this->search_function);
                                                     ;do_directories($this->search_function);
                                                                      🎜>                                                                                                                // End of class
?>

//The following is an example of calling this class, please save it as example.php


include('search_replace.inc'); //Include the file

//Create a new object, set the search conditions, and finally return the search results

$sr = new search_replace('asp' , 'php', array('test.txt')); // Call search and replace
$sr->set_search_function('quick'); //Set search conditions
$sr->do_search ();

$sr->set_find('another');
do_search();

//The following is the customized return information
header('Content-Type: text/plain');
echo 'Find and replace the following places: '.$sr->get_num_occurences()."rn";
echo 'Ah, an error occurred As follows.............: '.$sr->get_last_error()."rn";
?>

//Save the following text as test .txt, please note that 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 really good."

At this time, if you open exame.php, the following will appear:
Discover and replace the following places:3
Ah, the error occurred as follows...: In the test.txt file, as expected, the place where asp appears was replaced by php.

http://www.bkjia.com/PHPjc/316312.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316312.htmlTechArticleThis is a very useful program that can perform specific searches on text files and replace specified text with specific text. Text, for example, if I type one word in this article...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn