replace.php
上传用户:yttaitong
上传日期:2009-05-10
资源大小:128k
文件大小:3k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

PHP

  1. <?
  2. class ReplaceFile{
  3. var $msg="";
  4. var $sub;
  5. var $backPath;
  6. var $arrFileType;
  7. var $arrSearch;
  8. var $arrReplace;
  9. var $intCount;
  10. var $isReg;
  11. var $subfolder;
  12. var $repNum=0;
  13. var $allowBack;
  14. function ReplaceFile($arrConfig){
  15. $this->sub            = $arrConfig['sub'];
  16. $this->isReg          = $arrConfig['isReg'];
  17. $this->subfolder      = $arrConfig['subfolder'];
  18. $this->strReplacePath = $arrConfig['replacePath'];
  19. $this->arrFileType = $arrConfig['fileType'];
  20. $this->arrSearch   = $arrConfig['search'];
  21. $this->arrReplace   = $arrConfig['replace'];
  22. $this->intCount   = 0;
  23. if ($this->checkConfig()){
  24. $startTime = time();
  25. $this->flushStatus('start');
  26. $this->getFilePath($this->strReplacePath);
  27. $time = date('H小时i分s秒', mktime(0, 0, (time()-$startTime)));
  28. $note = "替换结束,";
  29. $note.="共替换".$this->repNum."处,";
  30. $note.="共替换文件".$this->intCount."个,耗时".$time."</font>";
  31. $this->flushStatus($note);
  32. }
  33. echo $this->msg;
  34. }
  35. function checkConfig(){
  36. if (!is_array($this->arrFileType) || !is_array($this->arrSearch) || !is_array($this->arrReplace)){
  37. return false;
  38. }
  39. return true;
  40. }
  41. function getFilePath($path){
  42. if (is_dir($path)){
  43. $arrDir = scandir($path);
  44. foreach ($arrDir as $file){
  45. if (is_dir($path.$file)){
  46. if($this->sub){
  47. if ($file != "." && $file != ".."){
  48. $this->getFilePath($path.$file.'/');
  49. }
  50. }
  51. }
  52. else{
  53. $path_parts = pathinfo($path.$file);
  54. if (array_search($path_parts['extension'], $this->arrFileType) !== false){
  55. if(!$this->rewriteFile($path.$file)){
  56. $this->flushStatus('error'.$file);
  57. }
  58. }
  59. }
  60. }
  61. }
  62. }
  63. function rewriteFile($filePath){
  64. $fileContent = file_get_contents($filePath);
  65. foreach ($this->arrSearch as $key=> $search){
  66. if(!$this->isReg){
  67. $fileContent = str_replace($search, $this->arrReplace[$key], $fileContent,$count);
  68. }
  69. else{
  70.   $count=preg_match_all("/".$search."/",$fileContent,$tmp);
  71. $fileContent = preg_replace("/".$search."/", $this->arrReplace[$key], $fileContent);
  72. }
  73. if($count>0){
  74. $this->intCount++;
  75. $this->flushStatus($filePath." 替换个数:".$count."个<br />");
  76. }
  77. $this->repNum+=$count;
  78. }
  79. if (!$handle = @fopen($filePath, 'w')){
  80. return false;
  81. }
  82. if (@fwrite($handle, $fileContent) === false){
  83. fclose($handle);
  84. return false;
  85. }
  86. fclose($handle);
  87. return true;
  88. }
  89. function flushStatus($status){
  90. if ($status == 'start'){
  91. $this->msg.="<font color=#FFFFFF>文件正在替换...... <br />替换目录:".$this->strReplacePath."<br />正则: ".($this->isReg?"是":"否")."<br />文件类型:".@implode('、', $this->arrFileType)."<br />";
  92. foreach ($this->arrSearch as $key=>$search){
  93. $this->msg.=$search.' 替换为 '.$this->arrReplace[$key]."<br />";
  94. }
  95. $this->msg.="开始替换<br />";
  96. }
  97. else{
  98. $this->msg.=$status;
  99. }
  100. flush();
  101. }
  102. }
  103. ?>