Hi I have a sting like this $a="test test";
This string I pass to a javascriptfunction escape('<?php echo $a ?>') for which I get the following error
Error: unterminated string literal Source Code: return escape('test
This error is because javascript considers end of line as end of a statement. How can I resolve this error? when I echo $a i get test test. I tried to replace the space between the two test but couldnot succeed. Can someone help.
What if you set $a = "test\ntest";
or perhaps $a = "test\ntest"; in view that PHP would probably parse the former into an actual newline.
The problem is the string $a is the content of the textarea. It depends on the user filling the textarea. If he doesnt hits "enter" in the textarea all is fine. But if he hits "enter" ....there lies the problem
Have you tried nl2br()?
ya tried that too. Gives same error 🙁
Oh yeah, because nl2br() should add the XHTML breaks while leaving the newline sequences in place. Sorry about that.
You just have to do a direct str_replace() then.
preg_replace( "\n"," ", $a);
In my mind this would replace a newline that you get with a space character.
For str_replace i.e mixed str_replace ( mixed search, mixed replace, mixed subject [, int &count])
I did $a=str_replace("\n"," ",$a); But the same error exists
What shud I specify for mixed search??
It depends on your system.
On *nix systems, use \n On Windows systems use \r\n On (older) Mac systems, use \r
Or str_replace("\n","\\n",$a) - this will end each line of the string with a backslash, which will tell Javascript that the line does continue (Javascript will ignore the newline; if you want one, you'll need to write one: easiest way to do that is just add a backslash: str_replace("\n","\n",$a)).
Assuming $a="test test"; for str_replace("\n","\\n",$a); the microsoft script debugger prints escape('test \ test'); with error: unterminated string constant for str_replace("\n","\n",$a); the microsoft script debugger prints escape('test \ntest'); with the same error
seems you'll be on a windows-based system then.... cause there is a line break being printed altho you are escaping the \n.... so try this
$a = "test test"; $a = str_replace("\r\n","\\r\\n",$a);