I'm generating a JSON file using PHP's json_encode function, and I've escaped double-quote characters with "
, so the output line looks like this:
"apply_instructions":"<p>Visit <a href="http:\/\/www.google.com">www.google.com<\/a><\/p>"
I'm then using JQuery's getJSON
function to retrieve and loop through the file. I'm attempting to unencode the "
using this:开发者_C百科
entry.apply_instructions = entry.apply_instructions.replace('"', '"');
For whatever reason, it's not working. The first quote mark seems to be getting replaced, but the second one won't budge. I've tried using other random find-and-replace characters, with similar results.
.replace()
only replaces the first instance, you need to use regex to do a global replace.
IE:
.replace(/"/g, "\"");
Try not escaping the quotes (in php):
$arr = array(
"apply_instructions"=>'<p>Visit <a href="http://www.google.com">www.google.com</a></p>'
);
echo json_encode($arr); // it will do all the escaping for you
See demo: http://codepad.org/VzT99sqM
You can either use regex to replace all instances of "
.
Or use a different function to escape the quotes using backslashes.
精彩评论