PHP JSON String, escape Double Quotes for JS output PHP JSON String, escape Double Quotes for JS output json json

PHP JSON String, escape Double Quotes for JS output


Another way would be to encode the quotes using htmlspecialchars:

$json_array = array(    'title' => 'Example string\'s with "special" characters');$json_decode = htmlspecialchars(json_encode($json_array), ENT_QUOTES, 'UTF-8');


I succefully just did this :

$json = str_replace("\u0022","\\\\\"",json_encode( $phpArray,JSON_HEX_QUOT)); 

json_encode() by default will escape " to \" . But it's still wrong JSON for json.PARSE(). So by adding option JSON_HEX_QUOT, json_encode() will replace " with \u0022. json.PARSE() still will not like \u0022. So then we need to replace \u0022 with \\". The \\\\\" is escaped \\".

NOTE : you can add option JSON_HEX_APOS to replace single quote with unicode HEX value if you have javascript single quote issue.

ex: json_encode( $phpArray, JSON_HEX_APOS|JSON_HEX_QUOT ));