简体   繁体   中英

Replace variables in all php files

I have a tough question about replacing certain variable name across all php files, ie 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.

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, eg 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']}");

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.

在此处输入图片说明

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";
--------------------------------------------------------------------------------

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM