无限递归替换文件内的某个字符串

Posted MiraclesGG

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无限递归替换文件内的某个字符串相关的知识,希望对你有一定的参考价值。

// 把这两个文件放到一个文件夹,然后放到要更改的文件根目录。

像这样:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SearchMatchStr</title>
</head>
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
<body>
<div id="container">
    <form action="searchReplaceStr.php" method="post" @submit="onSubmit">
        <input type="text" name="target" v-model="target">
        <span style="color:red;">替换为</span>
        <input type="text" name="strReplace" v-model="strReplace">
        <button>Submit!</button>
    </form>
</div>
</body>
<script type="text/javascript">
    var app = new Vue({
        el: \'#container\',
        data: {
            target: null,
            strReplace: null,
        },
        methods: {
            onSubmit: function (e) {
                if (this.target && this.strReplace) {
                    return true;
                }
                e.preventDefault();
            }
        },
    })
</script>
</html>

// php脚本文件

<?php
class searchReplaceStr{
    private $search = null;
    private $matchStr = null;
    public function __construct ($search, $matchStr) {
        if ($search == null || $matchStr == null) {
            return false;
        }
        $this->search = $search;
        $this->matchStr = $matchStr;
    }
    private function modifyFile ($filename) {
        $fileHandler = fopen($filename, \'r+\');
        $modify = \'\';
        while (!feof($fileHandler)) {
            $source = fgets($fileHandler);
            $modify .= str_replace($this->search, $this->matchStr, $source);
        }
        if (!@unlink($filename)) {
            echo "delete file " . $filename . " success<br>";
        }
        $newFileHandler = fopen($filename, \'w\');
        if (!feof($newFileHandler)) {
            fwrite($newFileHandler, $modify);
        }
        if (file_exists($filename)) {
            echo "create file " . $filename . " success<br>";
        }
    }
    public function sourceDir($dir) {
        $files = array();
        if ($dir != ".idea" && $dir != \'serchReplaceStr\') {
            if(@$handle = opendir($dir)) {
                while(($file = readdir($handle)) !== false) {
                    if($file != ".." && $file != "." && $file != "searchReplaceStr.php" && $file != \'index.html\') {
                        if(is_dir($dir."/".$file)) {
                            $files[$file] = $this->sourceDir($dir."/".$file);
                        } else {
                            $this->modifyFile($dir . "/" .$file);
                        }
                    }
                }
            }
        }
    }
}
if (!is_null($_POST)) {
    $searchReplaceStr = new searchReplaceStr($_POST[\'target\'], $_POST[\'strReplace\']);
    print_r($searchReplaceStr->sourceDir(\'../\'));
} else {
    echo "<script>
            alert(\'Please input value\');
            location.href = \'./index.html\';
          </script>";
}

 

以上是关于无限递归替换文件内的某个字符串的主要内容,如果未能解决你的问题,请参考以下文章

如何替换Notepad ++中两个括号内的字符?

用活动组内的另一个片段替换片段

删除无限循环的文件夹-删除递归文件夹

用Python在给定目录及其子目录中递归替换文件中的字符串?

Python递归遍历文件夹批量替换某字符串改名

Python递归遍历文件夹批量替换某字符串改名