views:

509

answers:

2

i'm using jquery to output the results of a json string created by php from a database,

the only problem is, that some of the data is on multiple lines... how would i get around this causing a unterminated string literal error in javascript?

+2  A: 

Escape line endings by replacing "\n" with "\\n" and "\r" with "\\r". You will also want to escape single or double quotes, depending on which you are using to delimit the string.

Jeff Ober
I think you need to edit your answer because you obviously don't want to replace "\r" with "\r" I think.
Clement Herreman
Heh, needed to do a bit of escaping myself :)
Jeff Ober
this is an idea, however there are no \n to escape... its all basically data from a database so there are no physical line breaks in there unless i change all new lines to \\n on data entry... but how would i go about that?
Neil Hickman
Line breaks *are* those characters. If you perform a regular expression replacement or string replacement using those characters, it will work.
Jeff Ober
i'm doing a str_replace() on the post var before database insertion replacing the characters as you said above, however its still putting new lines in the database and thus the string is being put over multiple lines which in javascript is not allowed.
Neil Hickman
Not in input - escape the data on output. If it does not work, post your code so we can troubleshoot it.
Jeff Ober
http://pastie.org/625794
Neil Hickman
What does the JSON output look like? json_encode should be properly escaping line strings.
Jeff Ober
{"title":"testing again","copy":"all\r\n\r\nadvisers\r\n\r\nat\r\nnewtown","importance":"read Now","0":"date","1":"2009-09-22 13:12:22"}
Neil Hickman
That came out without newlines being escaped. 1) did stackoverflow remove extra escapes (like my initial response)? 2) are you setting the HTTP content-type header to "text/javascript"?
Jeff Ober
no stackoverflow didn't change anything.. thats exactly how it is on my screen. and the content type is correct also.
Neil Hickman
Then json_encode does not appear to be escaping its output correctly. It could be a bug in the version you are using. Before passing it to json_encode, replace instances of "\r" and "\n" in likely strings with doubly-escaped versions.
Jeff Ober
{"title":"testing again","copy":"all\r\\n\r\\nadvisers\r\\n\r\\nat\\r\\nnewtown","importance":"read Now","0":"date","1":"2009-09-22 13:12:22"}thats what i'm getting now by doing a preg_replace and its still coming back with the string literal unterminated... this generally happens when a string is on more than one line so it's obviously not the problem here with the line breaks
Neil Hickman
You missed most of the "\r"s
Jeff Ober
Great, i've used larryb82's preg_replace to change \r into \\n\\n that as for some reason it wasn't parsing the \r so now it works... thanks to both of you guys... Cheers!
Neil Hickman
+2  A: 

The following code will get rid of all \r and \n characters.

preg_replace('/[\r\n]+/', "", $stringFromDB)
Lawrence Barsanti