F在文件中查找并替换


FInd and replace in file

我试图做一个非常简单的搜索和替换php脚本,但它不起作用。你能告诉我我在这里做错了什么吗?

if(isset($_GET['fnr'])) {
    $find = $_GET['find'];
    $replace = $_GET['replace'];
    $path_to_file = 'index.php';
    $file_contents = file_get_contents($path_to_file);
    $file_contents = str_replace("$find","$replace",$file_contents);
    file_put_contents($path_to_file,$file_contents);
}

$file_contents = str_replace("$find","$replace",$file_contents); 中删除"

textfile.php

我的名字叫Frayne,我的名字也叫konok

PHP

  $find = "My name";
  $replace = "name";
  $path_to_file = 'textfile.php';
  $file_contents = file_get_contents($path_to_file);
  $file_contents = str_replace($find,$replace, $file_contents);
  file_put_contents($path_to_file,$file_contents);

输出:

我叫Frayne,也叫konok

输出字符串从文件中删除My