I am trying to format a PHP string variable for use in Javascript. Problem is that this string could contain line breaks, single quotes, double quotes and the like which will break Javascript.
for example, How do I replace a line break (\n) with the actual text "\n"???
Does anybody have any experience with this?
Thanks in advance,
-Mike
To replace a newline character with a literal \n, use str_replace() and escape the slash.
$string = str_replace("\n","\n",$string);
If you still have problems, make sure your string doesn't have carriage returns ( \r ) as well.