多个文件中的字符串替换

String replacement within multiple files

我有这个功能,可以在 php 文件中搜索和替换给定的术语。 当我传递一个包含多个文件的数组时,文件没有被修改,为了避免这个问题,我一次只能传递两个文件。我该如何修复或改进此功能?我认为问题是因为多个文件会占用太多内存,但我不确定这一点。任何帮助表示赞赏。 (我正在使用 wordpress 安装来测试该功能,以查找和替换某些文件夹和文件的 wp-prefix)。

function wp_settings_edit(array $filename, array $patterns, array $replace){
  foreach($filename as $file){
    $wp_file = file_get_contents(__DIR__."/{$file}");
    foreach($patterns as $pattern){
      foreach($replace as $r){
      $wp_file_content = file_put_contents(__DIR__."/{$file}",str_replace("{$pattern}", "{$r}", $wp_file));
      }
    }
  }
}

用法示例:

 $files = array('wp-settings.php', 'wp-load.php');
 $patterns = array('wp-admin', 'wp-includes');
 $replace = array('admin', 'includes');
 wp_settings_edit($files, $patterns, $replace); 

除非您的文件非常大——比如数百兆字节——否则您肯定不会 运行进入内存限制。但是,您这样做效率很低。

您 运行 在 both 搜索和替换数组上进行 foreach 循环,使文件写入次数呈指数增长。如果 str_replace() 只接受字符串参数,您可以摆脱这些循环之一。但是,它接受数组参数,所以你可以去掉两者。

function wp_settings_edit(array $filename, array $patterns, array $replace){
    foreach($filename as $file){
        $wp_file = file_get_contents(__DIR__ . "/$file");
        file_put_contents(__DIR__ . "/$file", str_replace($patterns, $replace, $wp_file));
    }
}

这将提高效率,更重要的是,它实际上会完成您想要的工作。通过对两个数组进行嵌套循环,就像在原始代码中一样,您最终将只用 first 替换搜索数组中的 every 项替换数组的元素:

$s = ["foo", "bar", "baz"];
$r = ["oof", "rab", "zab"];
$h = "Here is some foo text as well as some bar text and maybe some baz text.";
foreach ($s as $s1) foreach ($r as $r1) $h = str_replace($s1, $r1, $h);
echo $h;

输出:

Here is some oof text as well as some oof text and maybe some oof text.

多个文件中的字符串替换。

    /*Multiple file str change */
     function multipleFileStrReplace($path,$string_to_replace,$replace_with){    
$fileList = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST); 
foreach ($fileList as $item) {
    /*
    for txt file is following if condition =>   if ($item->isFile() && stripos($item->getPathName(), 'txt') !== false) 
    for other extension file then =>  if ($item->isFile() && stripos($item->getPathName(), 'other extention') !== false) 
    for all files search then just =>  if ($item->isFile()) 
    */
    if ($item->isFile() && stripos($item->getPathName(), 'txt') !== false) 
    {
        $file_contents = file_get_contents($item->getPathName());
        $file_contents = str_replace($string_to_replace,$replace_with,$file_contents);
        file_put_contents($item->getPathName(),$file_contents);
    }
    }
    }
    $path = $_SERVER['DOCUMENT_ROOT']."/tests/replace/"; // Path to your textfiles 
    $string_to_replace="text you want to change";
     $replace_with="text you want to replace";
    multipleFileStrReplace($path,$string_to_replace,$replace_with);
      /* Single file sting change */
    function replace_string_in_file($filename, $string_to_replace, $replace_with){
    $content=file_get_contents($filename);
    $content_chunks=explode($string_to_replace, $content);
    $content=implode($replace_with, $content_chunks);
    file_put_contents($filename, $content);
    }
   $filename="ve.txt";
    $string_to_replace="text you want to change";
    $replace_with="text you want to replace";
   replace_string_in_file($filename, $string_to_replace, $replace_with);