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

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题