Gunah Gaar Gunah Gaar - 4 months ago 12
JSON Question

PHP JSON encode issue and retrieval issue

I usually do this to encode simple strings to be obtained via an AJAX request.

$str = '<img src="someinage.png" />';
echo json_encode(array('status'=>'OK',$str));


The output:

{"status":"OK","html":"<img src="someinage.png" />"}


See the double quotes, how can I avoid this issue?

Answer

Use the html_entity_decode function for the $str.

$str = '<img src="someinage.png" />';
echo json_encode(array('status'=>'OK',html_entity_decode($str)));