Click to See Complete Forum and Search --> : PHP newline character in Javascript, help!
luckybird
09-23-2006, 10:14 PM
A PHP variable retrieved from DB with newline characters in it(obtained from user input textarea).
I want to echo it in Javascript.
However, it reports error which comes from the 'newline', cause it actually breaks the line in the output javascirpt code.
I tried to replace the 'newline', but it seems that it's NOT '\n', '\n\r' or '\r\n'
what should I do? :confused:
thanks!
ronverdonk
09-24-2006, 09:03 AM
Show the instruction with which you tried to remove the newline(s).
Ronald :cool:
luckybird
09-24-2006, 12:41 PM
I used
str_replace('\n', '--', $str) // and, '\r', '\r\n', '\n\r'
The function nltobr() however, can locate the 'newline', correctly. But it doesn't remove it, just insert a <br> before it.
An alternative way might be trying to make the output of PHP varaible displayed in a single line by a certain way of encoding or sth. But I don't know how.
Any suggestions?
ronverdonk
09-24-2006, 03:16 PM
str_replace('\n', '--', $str) replaces 2 characters: the backslash and the r. In order to remove the newline you must put the to-be-replaced value within double quotes, because that interprets the \r correctly, i.e.
$str = str_replace("\n", '-', $str)
Ronald :cool:
luckybird
09-24-2006, 05:58 PM
That's it! And that's why 'newline' cannot be escaped by addslashes(), because it's what it means, it's not characters like '\n', '\r'...
Thank you Ronald!