码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-09-08 11:54:10      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:span   back   图片   vat   chm   无限   script   ||   oca   

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

像这样:

技术分享图片

<!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>";
}

 

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

标签:span   back   图片   vat   chm   无限   script   ||   oca   

原文地址:https://www.cnblogs.com/phpcurd/p/9608049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!