replace.php
资源名称:BBWPS.rar [点击查看]
上传用户:yttaitong
上传日期:2009-05-10
资源大小:128k
文件大小:3k
源码类别:
WEB源码(ASP,PHP,...)
开发平台:
PHP
- <?
- class ReplaceFile{
- var $msg="";
- var $sub;
- var $backPath;
- var $arrFileType;
- var $arrSearch;
- var $arrReplace;
- var $intCount;
- var $isReg;
- var $subfolder;
- var $repNum=0;
- var $allowBack;
- function ReplaceFile($arrConfig){
- $this->sub = $arrConfig['sub'];
- $this->isReg = $arrConfig['isReg'];
- $this->subfolder = $arrConfig['subfolder'];
- $this->strReplacePath = $arrConfig['replacePath'];
- $this->arrFileType = $arrConfig['fileType'];
- $this->arrSearch = $arrConfig['search'];
- $this->arrReplace = $arrConfig['replace'];
- $this->intCount = 0;
- if ($this->checkConfig()){
- $startTime = time();
- $this->flushStatus('start');
- $this->getFilePath($this->strReplacePath);
- $time = date('H小时i分s秒', mktime(0, 0, (time()-$startTime)));
- $note = "替换结束,";
- $note.="共替换".$this->repNum."处,";
- $note.="共替换文件".$this->intCount."个,耗时".$time."</font>";
- $this->flushStatus($note);
- }
- echo $this->msg;
- }
- function checkConfig(){
- if (!is_array($this->arrFileType) || !is_array($this->arrSearch) || !is_array($this->arrReplace)){
- return false;
- }
- return true;
- }
- function getFilePath($path){
- if (is_dir($path)){
- $arrDir = scandir($path);
- foreach ($arrDir as $file){
- if (is_dir($path.$file)){
- if($this->sub){
- if ($file != "." && $file != ".."){
- $this->getFilePath($path.$file.'/');
- }
- }
- }
- else{
- $path_parts = pathinfo($path.$file);
- if (array_search($path_parts['extension'], $this->arrFileType) !== false){
- if(!$this->rewriteFile($path.$file)){
- $this->flushStatus('error'.$file);
- }
- }
- }
- }
- }
- }
- function rewriteFile($filePath){
- $fileContent = file_get_contents($filePath);
- foreach ($this->arrSearch as $key=> $search){
- if(!$this->isReg){
- $fileContent = str_replace($search, $this->arrReplace[$key], $fileContent,$count);
- }
- else{
- $count=preg_match_all("/".$search."/",$fileContent,$tmp);
- $fileContent = preg_replace("/".$search."/", $this->arrReplace[$key], $fileContent);
- }
- if($count>0){
- $this->intCount++;
- $this->flushStatus($filePath." 替换个数:".$count."个<br />");
- }
- $this->repNum+=$count;
- }
- if (!$handle = @fopen($filePath, 'w')){
- return false;
- }
- if (@fwrite($handle, $fileContent) === false){
- fclose($handle);
- return false;
- }
- fclose($handle);
- return true;
- }
- function flushStatus($status){
- if ($status == 'start'){
- $this->msg.="<font color=#FFFFFF>文件正在替换...... <br />替换目录:".$this->strReplacePath."<br />正则: ".($this->isReg?"是":"否")."<br />文件类型:".@implode('、', $this->arrFileType)."<br />";
- foreach ($this->arrSearch as $key=>$search){
- $this->msg.=$search.' 替换为 '.$this->arrReplace[$key]."<br />";
- }
- $this->msg.="开始替换<br />";
- }
- else{
- $this->msg.=$status;
- }
- flush();
- }
- }
- ?>