Wrote a class to traverse the directory, bulk replace the contents of the solution of the idea

Source: Internet
Author: User
Write a class that traverses directories and replaces file contents in bulk
Write this class before you need it.
Function:
1 traversing all files in the directory (you can specify the suffix name)
2 Bulk Replace file contents (regular, String)
3 Batch replacement file suffix name
4 Batch Replacement file encoding

Use Example:
PHP Code
  
   $dirExplorer = new Direxplorerclass (); $dirExplorer->getdirexplorer (' d:/test1/test2/');                                  Traverse directory d:/test1/test2/$dirExplorer->modifyfileby_replace (' AA ', ' QQ ', ' shift-jis ', ' UTF-8 ', ' txt ', ' txt ');    Replace the AA in all file contents with QQ, convert the file encoding from Shift-jis to UTF-8, and change all txt suffix names to txt


Class Code:
PHP Code
  Class direxplorerclass{var $dirPath _array = Array ();//traversal file result set function __construct () {//donothing}/ * * Return a directory handle or Die */Private Function Opendir ($dirPath _target) {if (Is_dir ($dirPa        Th_target) {return opendir ($dirPath _target);        }else {die ("$dirPath _target are not a Directory"); }}/* * Close a directory handle */Private Function Closedir ($dirHander) {Closedir ($dirHande    R);     }/* * traverse the specified directory and return the collection of file names under it * * Parameters: * 1 Dirpath: The folder to traverse * 2 extension: Returns only files with the specified suffix name * Return: * Traverse file result set */function Getdirexplorer ($dirPath, $extension = ') {$dirHander = $this->op        Endir ($dirPath); while ($fileName = Readdir ($dirHander)) {if ($fileName! = '. ' && $fileName! = ') {$path = $dirPath. " /" .                $fileName; if (Is_dir ($path)) {$this->getdirexploreR ($path); }else{if (isset ($extension) && $extension! = ") {$fileExtension = end (E                        Xplode ('. ', $fileName));                        if ($fileExtension! = $extension) {continue;                }} $this->dirpath_array[]= $path;        }}} $this->closedir ($dirHander);    return $this->dirpath_array;    }/* * String replaces file contents (case sensitive), encoding, suffix * * Parameters: * 1 Search: String to replace (array available) * 2 Replace:      Replaced string (array available) * 3 In_charset: Original code * 4 Out_charset: New code * 5 in_extension: original suffix name * 6 out_extension: New suffix name * Return: * True or FALSE */function Modifyfileby_replace ($search, $replace, $ In_charset= ", $out _charset=", $in _extension= ", $out _extension=") {/* input check */if (!isse T ($search) | | !isset ($replace) || (Strlen ($in _charset)!=0 && strlen ($in _charset) ==0) | |            (Strlen ($in _charset) ==0 && strlen ($in _charset)!=0) | | (Strlen ($in _extension)!=0 && strlen ($out _extension) ==0) | | (Strlen ($in _extension) ==0 && strlen ($out _extension)!=0))        {return false; } foreach ($this->dirpath_array as $key = + $file) {$content = file_get_contents ($file);//read Conten            TS $content = Str_replace ($search, $replace, $content); if (strlen ($in _charset)!=0 && strlen ($out _charset)!=0) {/* Change the encode */$this            ->changecharset ($in _charset, $out _charset, 1, $content);            } unlink ($file);                if (strlen ($in _extension)!=0 && strlen ($out _extension)!=0) {/* change file ' s extension */            $this->changeextension ($in _extension, $out _extension, 1, $file); } file_put_contents ($filE, $content);            Unset ($content);        /* Update traverse file name result set */$this->dirpath_array[$key] = $file;    } return true; }/* * String replaces file contents (ignoring case), encoding, suffix */function modifyfileby_ireplace ($search, $replace, $in _charset= ", $out _    Charset= ', $in _extension= ', $out _extension= ') {//Do not post with the modifyfileby_replace above and just replace it with str_ireplace}/*    * Regular Replace file contents (ignoring case), encoding, suffix name * * Parameters: * 1 Search: Regular expression to replace content * 2 Replace: replaced string * 3 In_charset: Original code * 4 Out_charset: New code * 5 in_extension: Original suffix name * 6 out_extension: New post Prefix name * Return: * True or FALSE */function Modifyfileby_regex ($search, $replace, $in _charset= ", $out _ Charset= ", $in _extension=", $out _extension= ") {/* input check */if (!isset ($search) | |!isset             ($replace) | | (Strlen ($in _charset)!=0 && strlen ($in _charset) ==0) | | (Strlen ($in _charset) ==0 && strlen ($in _charset)!=0) | | (Strlen ($in _extension)!=0 && strlen ($out _extension) ==0) | | (Strlen ($in _extension) ==0 && strlen ($out _extension)!=0))        {return false; } if (Preg_match ('!) ( [a-za-z\s]+) $!s ', $search, $match) && (Strpos ($match [1], ' E ')!== false)) {/* Remove the Eval-modifier fro M $search */$search = substr ($search, 0,-strlen ($match [1]). Preg_replace ('![        E\s]+! ', ', $match [1]); } foreach ($this->dirpath_array as $key = + $file) {$content = file_get_contents ($file);//read Conten            TS $content = preg_replace ($search, $replace, $content); if (strlen ($in _charset)!=0 && strlen ($out _charset)!=0) {/* Change the encode */$this            ->changecharset ($in _charset, $out _charset, 1, $content);            } unlink ($file);          if (strlen ($in _extension)!=0 && strlen ($out _extension)!=0) {      /* Change file ' s extension */$this->changeextension ($in _extension, $out _extension, 1, $file);            } file_put_contents ($file, $content);            Unset ($content);        /* Update traverse file name result set */$this->dirpath_array[$key] = $file;    } return true;         }/* * Transform code * * Parameters: * 1 In_charset: Original code * 2 Out_charset: New code * 3 flag: 0 to the traversal of the resulting file conversion code 1 to the specified content conversion encoding * 4 content: Only when the flag is 1 use * Return: * True or false */FU Nction Changecharset ($in _charset= ", $out _charset=", $flag =0, & $content = ") {/* input check */if (Strl En ($in _charset) ==0 | |        strlen ($out _charset) ==0) {return false;                } if ($flag = = 0) {/* to the traversal of the resulting file conversion encoding */foreach ($this->dirpath_array as $file) { $content = file_get_contents ($file);//read Contents/* Change the encode */$conteNT = Iconv ($in _charset, $out _charset, $content);                Unlink ($file);                File_put_contents ($file, $content);            Unset ($content); }}else{/* to the specified content conversion code */if (strlen ($content)! = 0) {$content = Iconv ($in _chars            ET, $out _charset, $content);    }} return true;      }/* * Transform suffix name * * Parameters: * 1 in_extension: Original suffix name * 2 out_extension: New suffix name * 3 flag:0 to traverse the resulting file transformation suffix name 1 to the specified file name transform suffix name * 4 filename: only use * Return when flag is 1: * True O R false */function changeextension ($in _extension= ", $out _extension=", $flag =0, & $fileName = ") {/*        InOut Check */if (strlen ($in _extension) ==0 | | strlen ($out _extension) ==0) {return false;                if ($flag = = 0) {/* for the traversed file transform suffix */foreach ($this->dirpath_array as $key + = $file) { /* Change file ' sExtension */$tmp = explode ('. ', $file);                $nowExtension = Array_pop ($tmp);                    if ($nowExtension = = $in _extension) {$content = file_get_contents ($file);//read contents                    Unlink ($file); $file = Implode ('. ', $tmp). '.                    $out _extension;                    File_put_contents ($file, $content);                Unset ($content);            }/* Update traverse file name result set */$this->dirpath_array[$key] = $file; }}else{/* to the specified file name, transform the suffix name */if (strlen ($fileName)! = 0) {$tmp = explode ('. '), $fil                ENAME);                $nowExtension = Array_pop ($tmp); if ($nowExtension = = $in _extension) {$fileName = Implode ('. ', $tmp).                $out _extension;    }}} return true; }}
  • 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.