php exec shell命令与regexp匹配

I'm creating small algorithm to fix msgid entire source code so this working ok:

$ find . -name '*.php' | xargs perl -pi -e 's/\$t\(\047bad msgid\047\);/\$t\(\047good msg id\047\);/g'

I want to create execute inside foreach of php script eg:

$cmd = "find {$projectPath} -name '*.php' | xargs perl -pi -e 's/\$t\('\047'" . preg_quote($bad) . "\047\)/\$t\(\047" . preg_quote($good) . "\047\)/g'";
exec(escapeshellarg($cmd));

Its crazy i try everything, escapeshellarg, mix doubled quotes with single quotes, backslash quotes.. after 4h i can tell one fu*ck this and shell exec cmd. ps. fuc*k reg exp too

any idea to run this command inside exec method ?


Solution for find $t('misspel msgid') replace to $t('right msgid') inside entire project usind php, perl, exec (tested on mac)

+1h and 2rev, thanks for suggestion @Marc B:

$cmd = "find {$projectPath} -name '*.php' | xargs perl -pi -e 's|\\\$t\(\\047" . str_replace("'", "\\047", preg_quote($bad)) . "\\047\)|\\\$t\(\\047" . str_replace("'", "\\047", preg_quote($good)) . "\\047\)|g'";
exec($cmd);

A basic understanding of quoting would help...

The \ you have in your $cmd string defnitition are consumed by PHP.

escapeshallarg() only deals with quotes, so your $t in the xargs get passed through verbatim to the shell, so you end doing essentially the equivalent of having typed the following at the shell prompt:

$ find /project/path -name '*.php' | xargs .... 's/$t etc....'

The $t will get expanded by the shell, and since you don't have a $t defined in this new shell, your regexes turn into

's/('\047
   ^---hey, no $t value!

Try

$cmd = "find {$projectPath} -name '*.php' | blah blah s/\\$t etc...
                                                        ^^--double escape

One backslash will be consumed by PHP, leaving s/\$t in the string. When that gets pased to the shell, the \$t will get passed in to perl as $t and be treated as a perl variable...