php - fixing improperly escaped string in javascript -
i'm connecting php webservice returns urls of images in json (using php json_encode function).
i'm working within mootools 1.2.5 system, needs work on mobile phones.
the format of image node returned in data this:
"thumb": "<img src=\"http://photos.imgserv.com/201107222000000.jpg\" />" for reason iphone , android see null value when pass response.json them. can pass response.text object them no problem, json.decode fails since double quotes not escaped properly. if manually add double slashes on image tags this:
"thumb": "<img src=\\"http://photos.imgserv.com/201107222000000.jpg\\" />" it works designed. however, i'm having hell of time getting right regexp replace \" in original response.text \\".
is there "right" way handle response.json mobile phones, , alternately how can write string.replace() regex handle these escape chars?
thanks!
edit add:
here's 2 jsfiddles single , double backslash show issue:
single backslash (doesn't decode/parse properly) -- http://jsfiddle.net/qde6f/ double backslash (does decode/parse properly) -- http://jsfiddle.net/qde6f/1/
did try this...
var src = "\"thumb\": \"<img src=\\\"http://photos.imgserv.com/201107222000000.jpg\\\" />\""; alert(src); var dest = src.replace("\\\"","\\\\\""); alert(dest); ie, replace expression replace("\\\"","\\\\\""), replace \" \\"
ok, worked me in jsfiddler
var text = '[{"thumb": "<img src=\"http://photos.imgserv.com/201107222000000.jpg\" />" }]'; console.dir(text); var dest = text.replace(/(src=)(\")([^"]+)(\")/g,"$1\\\"$3\\\""); console.dir(dest); console.dir(json.decode(dest)); basically regex rewrites src="url" src=\"url\".
Comments
Post a Comment