在文件中替换和替换

时间:2016-04-03 09:32:03

标签: php

我试图进行一个非常简单的搜索并替换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);
}

1 个答案:

答案 0 :(得分:0)

"

中删除$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

相关问题