Page 1 of 1

Replace PHP variable name inside PHP files

Posted: Sun Jul 22, 2007 10:20 pm
by syahzul
hi,

i would like to change some variable name in my php file, for example $basePath to $myPath. this is my functions

Code: Select all

function patchFile( $file, $search, $replace ) {

     if ( $fp = fopen( $file, 'r' ) ) {
        $content = fread( $fp, filesize( $file ) );
         fclose($fp);
    }
    else {
         // return error message
    }
    if( strpos( $content, $value ) === true ) {
        //$this->_isPatch = true;
    }
     // we didn't find it, proceed to patch process
    else {

        $content = str_replace( $search, $replace, $content );

        if( $fp = fopen( $file, 'w' ) ) {
            fwrite( $fp, $content, strlen( $content ) );
            fclose( $fp );
            unset( $content );
            echo 'Done!';

        } else {
            unset( $content );
            // return error message
             echo 'Error!';
        }
    }
}
function start() {
    $file = "D:/XAMPP/xampp/htdocs/my/tryout/xsecure/administrator/auth.php";
    $search = "$basePath";
    $replace = "$mypath";   
   
    patchFile( $file, $search, $replace );
}
and inside auth.php

Code: Select all

$basePath 	= dirname( __FILE__ );
require( $basePath . '/sources.php' );

$basePath 	= dirname( __FILE__ );
require( $basePath . '/config.php' );

/* 
other code....
....
....
*/
so, i want to change require( $basePath . '/sources.php' ); to require( $myPath . '/my-sources.php' );. i can change the filename, no problem with it but not $basePath. the only problem is i cannot change the PHP variable name to something else.

thank you for your help.

Posted: Sun Jul 22, 2007 10:48 pm
by superdezign
:?:

Code: Select all

$myPath = $basePath;
require( $myPath . '/my-sources.php' );
Your question makes no sense to me at all.

i want to make it automatically

Posted: Sun Jul 22, 2007 11:02 pm
by syahzul
okay, sorry maybe i'm not explaining it clearly.

i want to use this code for my wordpress. i want to change wp-config.php file outside /public_html/, so i need to search every file that require the config file, and change it to new location. and every time i install new plugin, this code will check plugin files if they got code to be change like above.

but it's ok, i got the solution already, by changing

Code: Select all

$search = "$basePath";
    $replace = "$mypath";
to

Code: Select all

$search = '$basePath';
    $replace = '$mypath';

thank you for replying.

Posted: Sun Jul 22, 2007 11:05 pm
by superdezign
That solution doesn't make any sense either but... glad to know you figured out a way to do whatever it is that you are after.

Posted: Mon Jul 23, 2007 12:14 am
by Benjamin
He wrote a find & replace script. Look at the code.

Posted: Mon Jul 23, 2007 6:05 am
by superdezign
astions wrote:He wrote a find & replace script. Look at the code.
Ahh. I don't usually see that done through PHP.