【问题标题】:Escaped Quotes in PHP for Sed Command用于 Sed 命令的 PHP 中的转义引号
【发布时间】:2012-05-03 13:45:55
【问题描述】:

我有以下 php 代码

if (!($stream = ssh2_exec($con, 'sed -i \'s/motd=A Minecraft Server/motd=\'.$name.\'s     Server/g\' /home/servers/runner15/server.properties
'))) {

但是当我运行它而不是将文本“motd=A Minecraft Server”替换为“motd=runner15s Server”时,它会将其更改为 motd=..s Server

这与转义引号有关

哦,顺便说一下 $name 包含 runner15

【问题讨论】:

    标签: php syntax sed quotes libssh2


    【解决方案1】:

    基本 PHP 字符串...' 字符串不会插入变量,因此您的 sed 命令包含文字 $name 等... ' 将被解释为远程服务器上不存在的 shell 变量,并扩展为空字符串。

    [...snip...]otd=A Minecraft Server/motd=\''.$name.'\'s [..snip...]
                                              ^-------^--- 'exit' the string in your client-side PHP
    

    【讨论】:

    • 我对所使用的词汇有点困惑,你能再给我解释一下吗?非常感谢您的帮助!
    • $name 未被替换为 '$name'(单引号!)
    【解决方案2】:

    在 ssh2_exec 之前尝试 escapeshellcmdhttp://www.php.net/manual/en/function.escapeshellcmd.php

    【讨论】:

      猜你喜欢
      • 2021-07-22
      • 2010-11-18
      • 1970-01-01
      • 2013-04-05
      • 2019-04-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 2020-01-31
      相关资源
      最近更新 更多