Question

[Solved] Understanding what u0000 is in PHP / JSON and getting rid of it

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:

hellou0000there

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 byte’ but I still can’t work out what to do.

Solution #1:

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("", $array[0]); // PHP doesn't use the same notation as JSON
Respondent: Alix Axel

Solution #2:

The string you have is "helloworld", or "hellox00world" whatever you prefer. If you echo it, the null symbol 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.

Respondent: Havenard

Solution #3:

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

Respondent: roy

Solution #4:

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

Respondent: mishik

Solution #5:

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));
Respondent: Sorin

Solution #6:

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.

Respondent: cwurtz

Solution #7:

In my case I’ve found the symbol inside serialized Laravel job’s payload json, something like s:8:"*order"; (or s:8:"u0000*u0000order";) which meant that serialized object’s property order has visibility protected on a moment of serialization

Respondent: antongorodezkiy

Solution #8:

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

$data = (array)json_decode(str_replace('u0000*u0000', '', json_encode($data)));
Respondent: Ezequiel García

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy