Welcome Guest, Not a member yet? Register   Sign In
Line breaks in javascript
#1

[eluser]richzilla[/eluser]
Hi all,
Im having trouble with line breaks messing up my javascript with unterminated string errors. The string in question is being pulled directly from the database and echo'd into my Javascript with php. Is there any way to echo line breaks out as "\n" instead of them being interpreted as line breaks?

Thanks
#2

[eluser]hagbard Celine[/eluser]
use str_replace before you echo out your string
#3

[eluser]Prophet[/eluser]
If you are using double quotes in the echo, PHP will just throw out a line break. Use single quotes and it will print '\n'. You could also try using the HTML character code for \

Try:
echo '\n'; - NOT echo "\n";

Or:
echo '& #92 ;n';
#4

[eluser]richzilla[/eluser]
Got it, Thanks for the help.

I used:
Code:
preg_replace("/\n/", '\n', $string);

and it seemed to do the trick.
#5

[eluser]Prophet[/eluser]
Great that you got it to work Smile

Always remember that when a string is enclosed in double quotes, PHP will interpret special characters and variables. With single quotes, it just takes what you give it and doesn't interpret anything.




Theme © iAndrew 2016 - Forum software by © MyBB