
how to solve json encoding problems with php.
Hello again..if you ever work with json in php..maybe you ran into some problems with json_encode() function like me .
so in this tutorial i like to share two problems i ran into.and how to solve them
1-First problem when json_encode return not valid code on chrome and work good with firefox or other browsers
The answer : go to notebad++ , choose the "Encoding" menu, then "Encode in UTF-8 without BOM" or with phpstorm go to “file”,then remove bom
2- the second problem with jeson_encode function to return not valid code when try to convert array which has contained special characters or html tags .
The answer : use the second parameter to add constants to define and convert these tags
$a = array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9");
echo "Normal: ", json_encode($a), "\n";
echo "Tags: ", json_encode($a, JSON_HEX_TAG), "\n";
echo "Apos: ", json_encode($a, JSON_HEX_APOS), "\n";
echo "Quot: ", json_encode($a, JSON_HEX_QUOT), "\n";
echo "Amp: ", json_encode($a, JSON_HEX_AMP), "\n";
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
echo "All: ", json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS
| JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";
Also if you want to trace json errors and you dont know where it comes from you can use json_last_error with json_last_error_msg to show and trace the error