【问题标题】:How to have apostrophes in PHP string? [duplicate]如何在 PHP 字符串中添加撇号? [复制]
【发布时间】:2013-03-11 04:05:33
【问题描述】:

我的PHP 代码的一部分包括URL 字符串的构造,如下所示:

$msg = $_REQUEST["msg"];
$content =  'action=sendmsg'. 
                '&user='.rawurlencode($username). 
                '&password='.rawurlencode($password). 
                '&to='.rawurlencode($destination). 
                '&text='.rawurlencode($msg);

$msg 恰好包含撇号时,它会以"\'" 发送。

我需要怎么做才能确保PHP 没有插入反斜杠?

【问题讨论】:

    标签: php string url escaping


    【解决方案1】:

    您可能想查看stripslashes:http://php.net/manual/en/function.stripslashes.php

    【讨论】:

      【解决方案2】:

      假设您要发送$content 变量而不是仅仅剥离反斜杠,请考虑使用urlencode() 而不是rawurlencode()。此外,您可以对 $content 变量使用该函数一次。

      $content = urlencode($content);
      

      更新:urlencoderawurlencode 可能不适合您的情况。为什么不直接发送$content 而不进行 URL 编码?您如何发送我们的查询字符串?如果使用 cURL,则不需要对参数进行编码。

      【讨论】:

      【解决方案3】:

      你可以试试

      1. 斜线
      2. 或将字符串放入 "" 并在所需位置添加 '。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-04
        • 1970-01-01
        • 1970-01-01
        • 2015-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多