Skip to content
Advertisement

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.

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement