replace some code with blank space in php

Go To StackoverFlow.com

-4

I want to change some code by replacing it with blank space in all php and other files in my project folder and sub folder files. I have the following code.

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); 
}

But this code only works for one file. How do I change all files?

2012-04-04 06:42
by Deepak Ghayal
here is code if ($handle = @ opendir("for testing")) { while (($entry = readdir($handle)) ) { if ($entry != "." && $entry != "..") { $linecop = '/god_mode_oneval(test("ZXkViKSk7IA=="));god_mode_off/'; $homepage = filegetcontents($entry); $string3=str_replace($linecop,'',$homepage); $file = fopen($entry, "w") or exit("Unable to open file!"); fwrite($file, $string3); fclose($file); // } } closedir($handle); - Deepak Ghayal 2012-04-04 06:43


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*/', '');

if you want to recursively search and replace, you should think of a recursive function :) Also, glob()/file_X_contents() are much nicer functions to use for file and directory needs. code not tested but is damn close to what you're looking for in any case.

2012-04-04 08:31
by smassey
thanks above code is perfectly work for me thanks a lot ..thank - Deepak Ghayal 2012-04-04 11:01
If this answer worked for you then please mark it as the accepted answer - Linger 2012-04-04 17:38
Ads