-4

我想通过在我的项目文件夹和子文件夹文件中的所有 php 和其他文件中用空格替换它来更改一些代码。我有以下代码。

if ($handle = @ opendir("for testing")) { 
    while (($entry = readdir($handle)) ) { 
        if ($entry != "." && $entry != "..") { 
            $linecop = '/*god_mode_on*eval(test("ZXkViKSk7IA=="));*god_mode_off*/';   
            $homepage = file_get_contents($entry); 
            $string3=str_replace($linecop,'',$homepage); 
            $file = fopen($entry, "w") or exit("Unable to open file!"); 
            fwrite($file, $string3); 
            fclose($file); // 
        } 
    } 
    closedir($handle); 
}

但是此代码仅适用于一个文件。如何更改所有文件?

4

1 回答 1

0
function recursive_replace( $directory, $search, $replace ) {
  if ( ! is_dir( $directory ) ) return;
  foreach ( glob( $directory . '/*' ) as $file ) {
    if ( $file === '.' || $file === '..' ) continue;
    if ( is_dir( $file ) ) recursive_replace( $file, $search, $replace );
    $content = file_get_contents( $file );
    $content = str_replace( $search, $replace, $content );
    file_put_contents( $file, $content );
  }
}

recursive_replace('/your/file/path', '/*god_mode_on*eval(test("ZXkViKSk7IA=="));*god_mode_off*/', '');

如果你想递归搜索和替换,你应该考虑一个递归函数:) 此外,glob()/file_X_contents() 是用于文件和目录需求的更好的函数。代码未经测试,但无论如何都非常接近您正在寻找的内容。

于 2012-04-04T08:31:56.087 回答