写了个遍历目录、批量替换文件内容的类
之前有需要,就写了这个类。
功能:
1 遍历目录下的所有文件(可指定后缀名)
2 批量替换文件内容(正则、字符串)
3 批量替换文件后缀名
4 批量替换文件编码
使用例:
$dirExplorer = new DirExplorerClass();$dirExplorer->getDirExplorer('D:/test1/test2/'); //遍历目录D:/test1/test2/$dirExplorer->modifyFileBy_replace('aa','QQ','shift-jis','UTF-8','txt','TXT'); //将所有文件内容中的aa替换为QQ,文件编码从shift-jis转换为UTF-8,将所有txt的后缀名改为TXT
类代码:
class DirExplorerClass{ var $dirPath_array = array();//遍历文件结果集合 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 is Not a Directory"); } } /* * close a directory handle */ private function closeDir($dirHander) { closedir($dirHander); } /* * 遍历指定目录,返回其下的文件名集合 * * Parameters: * 1 dirPath:需要遍历的文件夹 * 2 extension:只返回指定后缀名的文件 * Return: * 遍历文件结果集合 */ 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(explode('.',$fileName)); if($fileExtension != $extension){ continue; } } $this->dirPath_array[]=$path; } } } $this->closeDir($dirHander); return $this->dirPath_array; } /* * 字符串替换文件内容(区别大小写)、编码、后缀名 * * Parameters: * 1 search: 需要替换的字符串 (数组可) * 2 replace: 替换后的字符串 (数组可) * 3 in_charset: 原编码 * 4 out_charset: 新编码 * 5 in_extension: 原后缀名 * 6 out_extension:新后缀名 * 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); 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); /* 更新遍历文件名结果集 */ $this->dirPath_array[$key] = $file; } return true; } /* * 字符串替换文件内容(忽略大小写)、编码、后缀名 */ function modifyFileBy_ireplace($search, $replace, $in_charset='', $out_charset='', $in_extension='', $out_extension=''){ //不贴了 和上面的modifyFileBy_replace一样 只是用str_ireplace替换而已 } /* * 正则替换文件内容(忽略大小写)、编码、后缀名 * * Parameters: * 1 search: 需要替换内容的正则表达式 * 2 replace: 替换后的字符串 * 3 in_charset: 原编码 * 4 out_charset: 新编码 * 5 in_extension: 原后缀名 * 6 out_extension:新后缀名 * 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); /* 更新遍历文件名结果集 */ $this->dirPath_array[$key] = $file; } return true; } /* * 变换编码 * * Parameters: * 1 in_charset: 原编码 * 2 out_charset: 新编码 * 3 flag: 0对遍历得到的文件转换编码 1对指定内容转换编码 * 4 content: 仅在flag为1时使用 * Return: * true or false */ function changeCharset($in_charset='', $out_charset='', $flag=0, &$content=''){ /* input check */ if (strlen($in_charset)==0 || strlen($out_charset)==0){ return false; } if($flag == 0){ /* 对遍历得到的文件转换编码 */ 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{ /* 对指定内容转换编码 */ if(strlen($content) != 0){ $content = iconv($in_charset, $out_charset, $content); } } return true; } /* * 变换后缀名 * * Parameters: * 1 in_extension: 原后缀名 * 2 out_extension: 新后缀名 * 3 flag: 0对遍历得到的文件变换后缀名 1对指定文件名变换后缀名 * 4 fileName: 仅在flag为1时使用 * Return: * true or 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){ /* 对遍历得到的文件变换后缀名 */ 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 contents unlink($file); $file = implode('.',$tmp).'.'.$out_extension; file_put_contents($file, $content); unset($content); } /* 更新遍历文件名结果集 */ $this->dirPath_array[$key] = $file; } }else{ /* 对指定文件名变换后缀名 */ if(strlen($fileName) != 0){ $tmp = explode('.',$fileName); $nowExtension = array_pop($tmp); if($nowExtension == $in_extension){ $fileName = implode('.',$tmp).'.'.$out_extension; } } } return true; }}
回复讨论(解决方案)
这个很强大,学习了~
lz你太伟大了。。。。。。。。
加分加分加分
不错
不过有些还是不规范
比如:modifyFileBy_replace 一时驼峰式,一时下划线分隔式
公用函数没用使用 public
私有函数开头应该以 _ 开头
不错
不过有些还是不规范
比如:modifyFileBy_replace 一时驼峰式,一时下划线分隔式
公用函数没用使用 public
私有函数开头应该以 _ 开头
对
是不太规范
是很不规范!!!
private function openDir($dirPath_target)
private function closeDir($dirHander)
这两个方法没有必要存在,在里面也只是调用原生的函数
建议在 递归方法(getDirExplorer)中回调工作方法,而不是构造整个目录树后再用工作函数处理。
并不是什么时候都需要返回目录树的,比如你的应用只是替换文件内容
学习了.
ftgyhu
是很不规范!!! 好像是这样
private function openDir($dirPath_target)
private function closeDir($dirHander)
这两个方法没有必要存在,在里面也只是调用原生的函数
建议在 递归方法(getDirExplorer)中回调工作方法,而不是构造整个目录树后再用工作函数处理。
并不是什么时候都需要返回目录树的,比如你的应用只是替换文……
打算重写的时候去掉这两个方法
工作方法的话,主要是希望这个类有一定通用性
例子中的函数有点功能大杂烩的味道、一次遍历把文本替换、编码、后缀名改变都执行了。但实际中也有可能只希望变换编码或者后缀,所以这两个其实是分别单独作为2个功能提供的。所以考虑把遍历和各功能分开来,自由选择调用。
打算加个__get,在任一个功能被执行完毕后,可随时返回最新的遍历结果集合。
不错学习下~
好的,谢谢!!!
学习一下
这个很强大,学习了~
学习。
突然发现我进错板块了 OMG
很好!学习了。
good!
引用 4 楼 yangball 的回复:
不错
不过有些还是不规范
比如:modifyFileBy_replace 一时驼峰式,一时下划线分隔式
公用函数没用使用 public
私有函数开头应该以 _ 开头
对
是不太规范
不管规范不规范,写出来就很强了,能写出来的有多少?
服务吧
/* * 遍历指定目录,返回其下的文件名集合 * * Parameters: * 1 dirPath:需要遍历的文件夹 * 2 extension:只返回指定后缀名的文件 * Return: * 遍历文件结果集合 */ 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(explode('.',$fileName)); if($fileExtension != $extension){ continue; } } $this->dirPath_array[]=$path; } } } $this->closeDir($dirHander); return $this->dirPath_array; }
好东西
yidingdianer,yebucuo,bucuo
学习了。。
呵呵不错,用glob可能会减少一些代码
这些功能其实比较常见,linux下会比较轻松,3,4行shell估计就可以搞定了
先慨叹一下,因为我写不出,没那个耐心
后问一个问题:
为什么不用SPL?
用SplFileInfo、SplFileObject和迭代器应该更好
另外,php处理windows下unicode的文件名还是死症,只能期待php开发者提高,所以目前代码必须考虑这个问题
耐心看完了。写得不错!
不过有些代码的效率应该可以再提高些,
比如end(explode('.',$fileName));如果改成substr(strrchr($fileName,'.'),1) ,这个也许效率会更高些!
不过有些代码的效率应该可以再提高些,
比如end(explode('.',$fileName));如果改成substr(strrchr($fileName,'.'),1) ,这个也许效率会更高些!
啊 对的
谢谢你的指教 重写的时候再优化一下
先慨叹一下,因为我写不出,没那个耐心
后问一个问题:
为什么不用SPL?
用SplFileInfo、SplFileObject和迭代器应该更好
另外,php处理windows下unicode的文件名还是死症,只能期待php开发者提高,所以目前代码必须考虑这个问题
谢谢指教
SplFileInfo、SplFileObject不了解 周末看看
没有看到我期望的功能,比如文件自动侦测编码,然后转换为目标编码...
楼主很强大,我以前也准备搞个的,想想算了,没那个时间.
给点积分吧
mark
hao
不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂不懂
多多学习吧
一个rename 就能解决啊
好东东
谢谢分享
学习,谢谢楼主~~
没看懂

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。
