gpt4 book ai didi

PHP Json_encode 将空格更改为加号 +

转载 作者:可可西里 更新时间:2023-11-01 01:07:59 24 4
gpt4 key购买 nike

我有一个网络应用程序,我首先将 JSON 数据存储在 cookie 中,然后每隔 x 秒保存到数据库中。它只是打开一个到服务器的连接,服务器读取 cookie。它实际上并不通过 POST 或 GET 发送任何东西。

当我保存到 cookie 时,我的数据格式正确。但是,当我在 PHP 中使用它然后设置一个新的 json_encoded 数组时,它用 + 符号替换空格,然后这些显示在我的网络应用程序中。我找不到任何方法来禁用 json_encode 的字符串编码,也找不到将这些加号解析出来的 JS 方法(使用 jQuery.parseJSON;stringify 的解析也不起作用)...有人知道吗:S?

最佳答案

来自fine manual :

Note that the value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned to a variable by the same name as the cookie name. If you don't want this, you can use setrawcookie() instead if you are using PHP 5.

但我认为您仍然需要对 cookie URL 进行编码,您只需要 %20 空格而不是 +。然而,urlencode :

[...] for historical reasons, spaces are encoded as plus (+) signs

您可以尝试使用 rawurlencode自己编码:

Returns a string in which all non-alphanumeric characters except -_.~ have been replaced with a percent (%) sign followed by two hex digits. This is the encoding described in RFC 3986 [...]

然后用setrawcookie来设置cookie。不幸的是,decodeURI 都没有, decodeURIComponent ,甚至是已弃用的 unescape JavaScript 函数会将 + 转换回空格;因此,您可能无法强制每个人以艰难的方式理解。

关于PHP Json_encode 将空格更改为加号 +,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6811845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com