[英]php exec shell command with regexp matching
I'm creating small algorithm to fix msgid entire source code so this working ok: 我正在创建小的算法来修复msgid整个源代码,因此可以正常工作:
$ 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: 我想在PHP脚本的foreach内部创建执行:
$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. 我疯狂地尝试了所有方法,escapeshellarg,将双引号与单引号,反斜杠引号混合在一起。.4h之后,我可以告诉我这个和shell exec cmd。 ps.
PS。 fuc*k reg exp too
fuc * k reg exp也
any idea to run this command inside exec method ? 有什么想法可以在exec方法中运行此命令吗?
Solution for find $t('misspel msgid') replace to $t('right msgid') inside entire project usind php, perl, exec (tested on mac) 解决方案查找$ T( 'misspel MSGID')替换为$ T( '右MSGID')内整个项目usind PHP,PERL,EXEC(在Mac上测试)
+1h and 2rev, thanks for suggestion @Marc B: + 1h和2rev,感谢@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. $ cmd字符串定义中的
\\
被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: escapeshallarg()
仅处理引号,因此您在xargs中的$t
逐字传递到shell,因此您基本上完成了等同于在shell提示符下键入以下内容的操作:
$ 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 $t
将通过shell扩展,并且由于您在此新shell中没有定义$t
,因此您的正则表达式将变为
'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. PHP将使用一个反斜杠,在字符串中保留
s/\\$t
。 When that gets pased to the shell, the \\$t
will get passed in to perl as $t
and be treated as a perl variable... 当将其粘贴到shell时,
\\$t
将作为$t
传递到perl并被视为perl变量...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.