Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding what \u0000 is in PHP / JSON and getting rid of it

Tags:

json

php

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.

like image 765
user2143356 Avatar asked Jul 06 '13 05:07

user2143356


People also ask

How check JSON format is correct or not in PHP?

To determine if the JSON output is genuine, PHP includes a method called json_decode(), which was introduced in PHP 5.3. It is a PHP built-in function that is used to decode a JSON string. It creates a PHP variable from a JSON encoded text.

How do I extract data from JSON with PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

What is Json_numeric_check?

JSON_NUMERIC_CHECK (int) Encodes numeric strings as numbers. JSON_PRETTY_PRINT (int) Use whitespace in returned data to format it.

What is the use of JSON encode in PHP?

The json_encode() function is used to encode a value to JSON format.


1 Answers

\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
like image 137
Alix Axel Avatar answered Sep 22 '22 16:09

Alix Axel