首頁 後端開發 php教程 写了个遍历目录、批量替换文件内容的类解决思路

写了个遍历目录、批量替换文件内容的类解决思路

Jun 13, 2016 am 10:00 AM
charset content file strlen

写了个遍历目录、批量替换文件内容的类
之前有需要,就写了这个类。
功能:
1 遍历目录下的所有文件(可指定后缀名)
2 批量替换文件内容(正则、字符串)
3 批量替换文件后缀名
4 批量替换文件编码

使用例:

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->$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
登入後複製


类代码:
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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;    }}<div class="clear">
                 
              
              
        
            </div>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

使用java的File.length()函數取得檔案的大小 使用java的File.length()函數取得檔案的大小 Jul 24, 2023 am 08:36 AM

使用Java的File.length()函數取得檔案的大小檔案大小是在處理檔案作業時很常見的一個需求,Java提供了一個很方便的方法來取得檔案的大小,即使用File類別的length()方法。本文將介紹如何使用此方法來取得檔案的大小,並給出對應的程式碼範例。首先,我們需要建立一個File物件來表示我們想要取得大小的檔案。以下是建立File物件的方法:Filef

鴻蒙原生應用隨機詩詞 鴻蒙原生應用隨機詩詞 Feb 19, 2024 pm 01:36 PM

想了解更多關於開源的內容,請造訪:51CTO鴻蒙開發者社群https://ost.51cto.com運行環境DAYU200:4.0.10.16SDK:4.0.10.15IDE:4.0.600一、建立應用程式點擊File- >newFile->CreateProgect。選擇模版:【OpenHarmony】EmptyAbility:填寫項目名,shici,應用包名com.nut.shici,應用儲存位置XXX(不要有中文,特殊字符,空格)。 CompileSDK10,Model:Stage。 Device

php blob怎麼轉file php blob怎麼轉file Mar 16, 2023 am 10:47 AM

php blob轉file的方法:1.建立一個php範例檔;2、透過「function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} 」方法實作Blob轉File即可。

使用java的File.renameTo()函數重命名文件 使用java的File.renameTo()函數重命名文件 Jul 25, 2023 pm 03:45 PM

使用Java的File.renameTo()函數重命名檔案在Java程式設計中,我們經常需要對檔案進行重命名的操作。 Java提供了File類別來處理檔案操作,其中的renameTo()函數可以方便地重新命名檔案。本文將介紹如何使用Java的File.renameTo()函數來重新命名文件,並提供對應的程式碼範例。 File.renameTo()函數是File類別的一個方法,

使用java的File.getParent()函數取得檔案的父路徑 使用java的File.getParent()函數取得檔案的父路徑 Jul 24, 2023 pm 01:40 PM

使用java的File.getParent()函數取得檔案的父路徑在Java程式設計中,我們經常需要操作檔案和資料夾。有時候,我們需要取得一個檔案的父路徑,也就是該檔案所在資料夾的路徑。 Java的File類別提供了getParent()方法用來取得檔案或資料夾的父路徑。 File類別是Java對檔案和資料夾的抽象表示,它提供了一系列操作檔案和資料夾的方法。其中,get

使用java的File.getParentFile()函數取得檔案的父目錄 使用java的File.getParentFile()函數取得檔案的父目錄 Jul 27, 2023 am 11:45 AM

使用java的File.getParentFile()函數取得檔案的父目錄在Java程式設計中,我們經常需要操作檔案和資料夾。當我們需要取得檔案的父目錄時,可以使用Java提供的File.getParentFile()函數來完成。本文將介紹如何使用這個函數並提供程式碼範例。 Java中的File類別是用於操作檔案和資料夾的主要類別。它提供了許多方法來取得和操作文件的屬性

如何使用Java中的File.delete()方法刪除檔案或目錄? 如何使用Java中的File.delete()方法刪除檔案或目錄? Nov 18, 2023 am 08:02 AM

如何使用Java中的File.delete()方法刪除檔案或目錄?概述:在Java中,我們可以使用File類別的delete()方法來刪除檔案或目錄。此方法用於刪除指定的檔案或目錄。但是要注意的是,該方法只能刪除空目錄或沒有被其他程式開啟的檔案。如果檔案或目錄刪除失敗,可以透過擷取IOException異常來找出特定原因。步驟一:導入相關的套件首先,我們需要

使用java的File.mkdirs()函數建立多層目錄 使用java的File.mkdirs()函數建立多層目錄 Jul 24, 2023 am 11:04 AM

使用Java的File.mkdirs()函數建立多層目錄在Java中,我們經常需要建立資料夾來儲存和組織檔案。而有時候,我們需要建立多層目錄,也就是包含子資料夾的資料夾。 Java提供了File類別的mkdirs()函數來實作這個功能。 File類是Java中處理文件和目錄的類,它提供了一系列操作文件和目錄的方法。其中,mkdirs()函數是建立多層目錄的函數。下

See all articles