I'm json_encoding some strings. Sometimes they contain binary data. This causes the encoding to fail with error code JSON_ERROR_UTF8. Running the strings through utf8_encode gets around this error. However, ✓ (a unicode checkmark) gets encoded as \u00e2\u009c\u0093 which when interpreted by JavaScript and rendered in your browser actually looks like â.
How can I fix this? Is there another encoding I can use?
echo json_encode(utf8_encode('✓')); // "\u00e2\u009c\u0093"
Now press F12 and paste that into your JavaScript console (quotes included). It should output â.
Please note that
echo json_encode('✓'); // "\u2713"
Works as intended. The issue is that sometimes the string will contain binary data which json_encode can't handle, so I need to sanitize every string without breaking the strings it can handle.
More examples:
json_encode(chr(200)); // false (bad)
json_encode(utf8_encode(chr(200))) // "\u00c8" (good)
json_encode('✓'); // "\u2713" (good)
json_encode(utf8_encode(chr(200))) // "\u00e2\u009c\u0093" (bad)
So you see, encoding it works well for some strings and breaks others.
This is strictly for logging. I don't care if the binary data comes out weird, I just don't want it to mess with valid strings.