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:
$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:
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 ($dirPath _target)) {return Opendir ($ Dirpath_target);} else {die ("$dirPath _target are not a Directory");}} /** close a directory handle */private function Closedir ($dirHander) {closedir ($dirHander);} /** iterates through the specified directory and returns the collection of file names under it * * parameters:* 1 Dirpath: Folder to traverse * 2 extension: Only files with the specified suffix name are returned * return:* Traverse file Result collection */functi On Getdirexplorer ($dirPath, $extension = ") {$dirHander = $this->opendir ($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 (Explode ('. '), $fileName)); if ($ FileExtension! = $extension) {continue;}} $this->dirpath_array[]= $path;}}} $this->closedir ($dirHander); return $this->d Irpath_array;} /** String Replacement 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 (!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;} foreach ($this->dirpath_array as $key + $file) {$content = file_get_contents ($file);//read contents$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);F (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;} /** Strings Replace file contents (ignoring case), encoding, suffix name */function modifyfileby_ireplace ($search, $replace, $in _charset= ", $out _charset=", $ In_extension= ', $out _extension= ') {//Don't post the same as above Modifyfileby_replace just replace it with Str_ireplace}/** Regular replace file contents (ignoring case), encoding, Suffix * * 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 suffix 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 Eval-modifier from $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 contents$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 traverse the resulting file conversion encoding 1 to the specified content conversion encoding * 4 content: Use only when flag is 1 * return:* true or FA LSE */function Changecharset ($in _charset= ", $out _charset=", $flag =0, & $content = ") {/* input check */if (Strle N ($in _charset) ==0 | | strlen ($out _charset) ==0) {return false;} if ($flag = = 0) {/* for the traversed 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 encoding */if (strlen ($content)! = 0) {$content = Iconv ($in _charset, $out _charset, $content);}} return true;} /** Transform suffix name * * parameters:* 1 in_extension: Original suffix name * 2 out_extension: New suffix name * 3 flag:0 pair of traversed file transform suffix name 1 to specified file name transform suffix name * 4 filename: only Use * return:* true or FALSE for flag 1 */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 name */foreach ($this->dirpath_array as $key + = $file) {/* change file ' s extension */$tmp = Explode ('. ', $file); $nowExtension = Array_pop ($tmp); if ($nowExtension = = $in _extension) {$content = file_get_contents ($ file);//read Contentsunlink ($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 filename transform suffix name */if (strlen ($fileName)! = 0) {$tmp = explode ('. ', $fileName); $nowExtension = Array_pop ($tmp); if ($ Nowextension = = $in _extension) {$fileName = Implode ('. ', $tmp). $out _extension;}}} return true;}}
Reply to discussion (solution)
This is very powerful, learning ~
LZ you are too great ...
Extra points plus points plus points
Not bad
But some are still not standardized.
For example: Modifyfileby_replace on the Hump-style, the moment underlined the split
Public functions are useless
The start of a private function should begin with _
Not bad
But some are still not standardized.
For example: Modifyfileby_replace on the Hump-style, the moment underlined the split
Public functions are useless
The start of a private function should begin with _
Right
is not very normative
is very not standardized!!!
Private Function Opendir ($dirPath _target)
Private Function Closedir ($dirHander)
There is no need for these two methods to exist, but only the native function is called.
It is recommended that you callback the working method in the recursive method (Getdirexplorer) instead of constructing the entire directory tree and then working with the work function.
It's not always necessary to return a directory tree, like your app just replaces the file content
Study.
Ftgyhu
is very not standardized!!! It's like that.
Private Function Opendir ($dirPath _target)
Private Function Closedir ($dirHander)
There is no need for these two methods to exist, but only the native function is called.
It is recommended that you callback the working method in the recursive method (Getdirexplorer) instead of constructing the entire directory tree and then working with the work function.
It is not always necessary to return to the directory tree, for example, your application is just an exchange of letters ...
I'm going to rewrite it and get rid of these two methods
Working methods, the main hope is that this class has a certain versatility
The function in the example is a bit of a mix of flavors, a traversal of text substitution, encoding, and suffix name changes are performed. In practice, however, it is possible to only change the encoding or suffix, so these two are actually provided separately as 2 functions. So consider separating the traversal and the functions, and choosing the call freely.
To add a __get, you can return the latest set of traversal results at any time after any function has been executed.
Good study Next ~
Ok thanks!!!
Learn a bit
This is very powerful, learning ~
Learn.
I suddenly found myself in the wrong plate, OMG.
Very good! Study.
good!
Reference 4 Floor Yangball's reply:
Not bad
But some are still not standardized.
For example: Modifyfileby_replace on the Hump-style, the moment underlined the split
Public functions are useless
The start of a private function should begin with _
Right
is not very normative
No matter the specification is not standardized, write out is very strong, can write out how many?
Service Bar
This post was last edited by xuzuning on 2011-06-24 13:44:35
/* * Traverse the specified directory and return the collection of file names under it * * Parameters: * 1 Dirpath: The folder to be traversed * 2 extension: Only files with the specified suffix name are returned * Re Turn: * Traversal file result set */function Getdirexplorer ($dirPath, $extension = ') {$dirHander = $this->opendir ($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; }
Good stuff.
Yidingdianer,yebucuo,bucuo
studied.
Oh good, with glob may reduce some code
These features are actually quite common, Linux will be easier, 3, 4 lines of shell estimates can be done
First sigh with regret, because I can not write, not that patience
After asking a question:
Why not use SPL?
It should be better to use Splfileinfo, Splfileobject and iterators
In addition, PHP processing under the Windows Unicode file name or certain, can only expect PHP developers to improve, so the current code must consider this issue
Patience to finish it. Well written!
But some of the code should be more efficient,
such as End (explode ('. ', $fileName)), if changed to substr (STRRCHR ($fileName, '. '), 1), this might be more efficient!
But some of the code should be more efficient,
such as End (explode ('. ', $fileName)), if changed to substr (STRRCHR ($fileName, '. '), 1), this might be more efficient!
Ah, right.
Thank you for your advice. Re-optimize it when you rewrite it.
First sigh with regret, because I can not write, not that patience
After asking a question:
Why not use SPL?
It should be better to use Splfileinfo, Splfileobject and iterators
In addition, PHP processing under the Windows Unicode file name or certain, can only expect PHP developers to improve, so the current code must consider this issue
Thank you for your advice
Splfileinfo, Splfileobject don't know about the weekend.
Did not see the features I expected, such as automatic file detection code, and then converted to the target encoding ...
The landlord is very strong, I was prepared to make a, think about it, not that time.
Give me some points.
Mark
Hao
Do not understand not understand do not understand do not understand do not understand do not understand do not understand do not understand don't understand do not understand don't understand don't understand do not know not understanding not understand do not know, do not understand, do not understand. Understand not understand do not understand do not understand do not understand do not understand don't understand don't understand don't understand do not understand don't understand don't understand do not know don't understand do not know do not understand, do not understand.
Learn a lot.
A rename can fix it.
Good stuff.
Thanks for sharing.
Study, Thank you landlord ~ ~
I can't read it.