Joe
Joe

Reputation: 15

Replace variables in all php files

I have a tough question about replacing certain variable name across all php files, i.e. to rename _$foo_ to _$bar["foo"]_ in all the php files. I'm planning to read all the php file as text and replace the desired variables, then write back the php file as text.

But the difficult point is, i cannot just replace $foo with $bar["foo"] directly, cases are:
1) _echo "hello $foo";_ <-- need to replace as:
_echo "hello ".$bar["foo"];_ or _echo "hello {$bar["foo"]}";_

2) _echo 'hello $foo';_ <-- no need to replace anything

I could imagine there will be many complex cases to handle. Wondering if there's any existing tools or libraries can do this kind of replace safely, or anyone have a better algorithm to handle? Thanks a lot.

Upvotes: 1

Views: 629

Answers (4)

Toto
Toto

Reputation: 91385

Here is a sample perl script that do the job for the examples you've given:

updated to accept many replacements.

#!/usr/bin/perl
use Modern::Perl;

my %repl = ('$foo' => '{$bar["foo"]}', '$baz' => '{$bar["baz"]}');
while(<DATA>) {
    chomp;
    say "before : $_";
    foreach my $key(keys %repl) {
        s/(_[^']*)(\Q$key\E)([^']*_)/$1$repl{$2}$3/;
    }
    say "after  : $_";
    say '-'x80;
}

__DATA__
_$foo_
_echo "hello $foo";_
_echo "hello ".$bar["foo"];_
_echo "hello {$bar["foo"]}";_
_echo 'hello $foo';_
$foo = "abc";
_$baz_
_echo "hello $baz";_
_echo "hello ".$bar["baz"];_
_echo "hello {$bar["baz"]}";_
_echo 'hello $baz';_
$baz = "abc";

output:

before : _$foo_
after  : _{$bar["foo"]}_
--------------------------------------------------------------------------------
before : _echo "hello $foo";_
after  : _echo "hello {$bar["foo"]}";_
--------------------------------------------------------------------------------
before : _echo "hello ".$bar["foo"];_
after  : _echo "hello ".$bar["foo"];_
--------------------------------------------------------------------------------
before : _echo "hello {$bar["foo"]}";_
after  : _echo "hello {$bar["foo"]}";_
--------------------------------------------------------------------------------
before : _echo 'hello $foo';_
after  : _echo 'hello $foo';_
--------------------------------------------------------------------------------
before : $foo = "abc";
after  : $foo = "abc";
--------------------------------------------------------------------------------
before : _$baz_
after  : _{$bar["baz"]}_
--------------------------------------------------------------------------------
before : _echo "hello $baz";_
after  : _echo "hello {$bar["baz"]}";_
--------------------------------------------------------------------------------
before : _echo "hello ".$bar["baz"];_
after  : _echo "hello ".$bar["baz"];_
--------------------------------------------------------------------------------
before : _echo "hello {$bar["baz"]}";_
after  : _echo "hello {$bar["baz"]}";_
--------------------------------------------------------------------------------
before : _echo 'hello $baz';_
after  : _echo 'hello $baz';_
--------------------------------------------------------------------------------
before : $baz = "abc";
after  : $baz = "abc";
--------------------------------------------------------------------------------

Upvotes: 1

dtech
dtech

Reputation: 14060

You can do this with php as follows:

file_put_contents("filename", preg_replace("#{?\$foo}?#", "{\$bar['foo']}", file_get_contents("filename")));

If it is one-time just use a decent text-editor, e.g. Notepad++.

[edit]After your comment:[/edit]

$orFile = file_get_contents("filename");
$newFile = preg_replace("#^(\s*)\$foo#U","\\1$bar['foo']",$orFile);
$newFile = preg_replace("#{?\$foo}?#", "{\$bar['foo']}");

Upvotes: 1

amosrivera
amosrivera

Reputation: 26514

What i usually do, and has saved me a lot of time, is use the replace in files built it in my editor. Easy and fast.

enter image description here

Upvotes: 1

DhruvPathak
DhruvPathak

Reputation: 43235

Either write a script in PHP or use advanced SED if you are on linux environment.

http://lowfatlinux.com/linux-sed.html

However I would recommend you write a script, as you might be having different types of cases, and this is a program code, not a news article, so no scope of syntax error.

Upvotes: 0

Related Questions