duan00529 2014-01-25 03:45 采纳率: 100%
浏览 60
已采纳

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);
  • 写回答

1条回答 默认 最新

  • dongmeng0317 2014-01-25 04:20
    关注

    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...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?