Ive encountered a weird problem when outputting an array using json_encode($string)
.
This problem arises if I call $name = mb_substr($name, 0, 100);
and the last character (at $name[100]) happens to be a special character on a string that is written to the database and then outputted with json_encode()
later.
I am encoding the string before hand with $string1 = mysql_real_escape_string ( $string );
and then $string1 = mb_convert_encoding($string1, "UTF-8", "auto");
to make sure the strings are UTF-8 (the DB charset is UTF-8 too). If I do this, I can store the new string in the database, SELECT it into an array, but json_encode()
fails.
If I do not split the string to start with, everything works fine.
I have already tried encoding with $response = array_map('utf8_encode', $response);
, json_encode ( $response, true )
and json_encode ( $response, JSON_UNESCAPED_UNICODE )
. However none of the above has worked.
So in essence my question is: How do get the substring of a string (from position 0 - 100) and ignoring a special character that may be split in half at the end of the string?
An example string that I have split that cannot be JSON encoded
🔶✨â‡ðŸ”³â—¼ðŸŒŸâ🔲🔳🔳⬅⬅🕚🕚⬇⬇â¬â¬…🕔🕔🕓🕓ðŸ•
If anyone can help me out on this one I would be most grateful. Cheers.