了解 \u0000 在 PHP / JSON 中是什么并摆脱它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17499955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding what \u0000 is in PHP / JSON and getting rid of it
提问by user2143356
I haven't a clue what is going on but I have a string inside an array. It must be a string as I have ran this on it first:
我不知道发生了什么,但我在数组中有一个字符串。它必须是一个字符串,因为我首先在它上面运行了这个:
$array[0] = (string)$array[0];
If I output $array[0] to the browser in plain text it shows this:
如果我以纯文本形式将 $array[0] 输出到浏览器,它会显示:
hellothere
But if I JSON encode $array I get this:
但是如果我 JSON 编码 $array 我得到这个:
hello\u0000there
Also, I need to separate the 'there' part (the bit after the \u0000), but this doesn't work:
另外,我需要将“那里”部分(\u0000 之后的位)分开,但这不起作用:
explode('\u0000', $array[0]);
I don't even know what \u0000 is or how to control it in PHP.
我什至不知道 \u0000 是什么,也不知道如何在 PHP 中控制它。
I did see this link: Trying to find and get rid of this \u0000 from my json...which suggests str_replacing the JSON that is generated. I can't do that (and need to separate it as mentioned above first) so I then checked Google for 'php check for backslash \0 byte' but I still can't work out what to do.
我确实看到了这个链接:试图从我的 json 中找到并摆脱这个 \u0000...这表明 str_replace 生成的 JSON。我不能这样做(并且需要像上面提到的那样首先将它分开),所以我随后检查了 Google 的“php 检查反斜杠 \0 字节”,但我仍然不知道该怎么做。
采纳答案by Alix Axel
\uXXXX
is the JSON Unicode escape notation (X
is hexadecimal).
\uXXXX
是 JSON Unicode 转义符号(X
是十六进制)。
In this case, it means the 0
ASCII char, aka the NUL byte, to split it you can either do:
在这种情况下,这意味着0
ASCII 字符,也就是 NUL 字节,要拆分它,您可以执行以下操作:
explode('\u0000', json_encode($array[0]));
Or better yet:
或者更好:
explode("echo str_replace('\u0000', "", json_encode($send));
", $array[0]); // PHP doesn't use the same notation as JSON
回答by Havenard
The string you have is "hello\0world"
, or "hello\x00world"
whatever you prefer. If you echo
it, the null symbol \0
won't be displayed, thats why you see helloworld
instead, but json_encode
will detect it and escape it as it does to any other special character, thats why its replaced by a visible \u0000
string.
您拥有的字符串是"hello\0world"
,或者"hello\x00world"
您喜欢的任何字符串。如果您使用echo
它,\0
则不会显示空符号,这就是您看到的原因helloworld
,但json_encode
会检测它并将其转义为任何其他特殊字符,这就是将其替换为可见\u0000
字符串的原因。
In my way of seeing it, json is encoding the string perfectly, the \u0000
is there to do its job of reproducing the inputted string in a json encoded way. You don't have to touch its output. If you don't want that \u0000
there you should fix its inputinstead.
在我看来,json 完美地编码了字符串,它\u0000
可以以 json 编码的方式再现输入的字符串。您不必触摸它的输出。如果你不想在\u0000
那里你应该修复它的输入。
回答by roy
you can simply do trim($str)
without giving it a charlist
你可以简单地做trim($str)
而不给它一个字符列表
回答by mishik
\uXXXX
is the unicode symbol with code XXXX
(hexadecimal).
For example: http://msdn.microsoft.com/en-us/library/aa664669(v=vs.71).aspx
\uXXXX
是带有代码的 unicode 符号XXXX
(十六进制)。例如:http: //msdn.microsoft.com/en-us/library/aa664669(v=vs.71).aspx
If you really get 0000
- then it's just the char with code 0
如果你真的明白0000
- 那么它只是带有代码的字符0
回答by Sorin
I came across this issue today and I sorted it out by replacing \u0000 in my array with "" before sending it back to the client.
我今天遇到了这个问题,我通过将数组中的 \u0000 替换为 "" 来解决它,然后再将其发送回客户端。
##代码##回答by antongorodezkiy
In my case I've found the symbol inside serialized Laravel job's payload json, something like s:8:"\0*\0order";
(or s:8:"\u0000*\u0000order";
) which meant that serialized object's property order
has visibility protected
on a moment of serialization
就我而言,我在序列化 Laravel 作业的有效负载 json 中找到了符号,类似于s:8:"\0*\0order";
(或s:8:"\u0000*\u0000order";
) 这意味着序列化对象的属性在序列化时刻order
具有可见性protected
回答by cwurtz
Try explode("\u0000", $array[0]);
, making sure you use double quotes. With single quotes it's going to parse the literal 6 character value.
尝试explode("\u0000", $array[0]);
,确保使用双引号。使用单引号,它将解析文字 6 个字符的值。
As others have mentioned, \u0000
is the Unicode NUL character.
正如其他人所提到的,\u0000
是 Unicode NUL 字符。