简体   繁体   English

替换所有PHP文件中的变量

[英]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. 关于在所有php文件中替换某些变量名,我有一个棘手的问题,即在所有php文件_$foo_重命名为_$bar["foo"]_ I'm planning to read all the php file as text and replace the desired variables, then write back the php file as text. 我打算读取所有的php文件为文本并替换所需的变量,然后将php文件写为文本。

But the difficult point is, i cannot just replace $foo with $bar["foo"] directly, cases are: 但是困难的是,我不能只用$ bar [“ foo”]直接替换$ foo ,情况是:
1) _echo "hello $foo";_ <-- need to replace as: 1) _echo "hello $foo";_ <-需要替换为:
_echo "hello ".$bar["foo"];_ or _echo "hello {$bar["foo"]}";_ _echo "hello ".$bar["foo"];__echo "hello {$bar["foo"]}";_

2) _echo 'hello $foo';_ <-- no need to replace anything 2) _echo 'hello $foo';_ <-无需更换任何东西

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: 您可以使用php进行以下操作:

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++ . 如果是一次性的,请使用像样的文本编辑器,例如Notepad ++

[edit]After your comment:[/edit] [edit]发表评论后:[/ 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. 我通常所做的并节省了很多时间,是使用在编辑器中建立的replace in files Easy and fast. 方便快捷。

在此处输入图片说明

Here is a sample perl script that do the job for the examples you've given: 这是一个示例perl脚本,用于完成您给出的示例:

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. 如果您使用的是Linux环境,请用PHP编写脚本或使用高级SED。

http://lowfatlinux.com/linux-sed.html 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. 但是,我建议您编写一个脚本,因为您可能遇到不同类型的情况,并且这是程序代码,而不是新闻文章,因此没有语法错误的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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