17

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:

hellothere

But if I JSON encode $array I get this:

hello\u0000there

Also, I need to separate the 'there' part (the bit after the \u0000), but this doesn't work:

explode('\u0000', $array[0]);

I don't even know what \u0000 is or how to control it in 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.

Community
  • 1
  • 1
user2143356
  • 5,467
  • 19
  • 51
  • 95

8 Answers8

13

\uXXXX is the JSON Unicode escape notation (X is hexadecimal).

In this case, it means the 0 ASCII char, aka the NUL byte, to split it you can either do:

explode('\u0000', json_encode($array[0]));

Or better yet:

explode("\0", $array[0]); // PHP doesn't use the same notation as JSON
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
12

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.

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

Havenard
  • 27,022
  • 5
  • 36
  • 62
  • How to fix the input, I have a UTF-8 encoded file - but no way I can find any null characters in there - please help – almaruf Aug 19 '16 at 11:27
  • @almaruf By removing the characters from the string before encoding it, of course. And yeah, null characters won't be displayed and give you the impression that they are simply not there, but at binary level they are and a `str_replace()` will be able to find and replace them. – Havenard Aug 19 '16 at 18:51
10

you can simply do trim($str) without giving it a charlist

Jaap
  • 81,064
  • 34
  • 182
  • 193
roy
  • 585
  • 5
  • 14
3

Just in case anyone need it to apply to the whole array

$data = (array)json_decode(str_replace('\u0000*\u0000', '', json_encode($data)));
Ezequiel García
  • 2,616
  • 19
  • 12
2

\uXXXX is the unicode symbol with code XXXX (hexadecimal). For example: 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

mishik
  • 9,973
  • 9
  • 45
  • 67
2

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.

echo str_replace('\\u0000', "", json_encode($send));
Sorin
  • 767
  • 12
  • 17
2

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

gorodezkiy
  • 3,299
  • 2
  • 34
  • 42
0

Try explode("\u0000", $array[0]);, making sure you use double quotes. With single quotes it's going to parse the literal 6 character value.

As others have mentioned, \u0000 is the Unicode NUL character.

icedwater
  • 4,701
  • 3
  • 35
  • 50
cwurtz
  • 3,177
  • 1
  • 15
  • 15