Nicolas
Nicolas

Reputation: 55

How do I let PHP echo "\n" as plain-text for javascript and not have the "\n" create a new line?

PHP is echoing JavaScript (I'm using the jQuery library) something like this:

echo 'var users = $("#add").val().split("\n");';

However, the \n is creating a line break in what the echoed script looks like, and therefore breaking the JavaScript. Is there a way to circumvent this?
Many thanks!

Upvotes: 4

Views: 2720

Answers (3)

Persk
Persk

Reputation: 669

Not sure If you looking for this

echo "<script>alert('Line1\\\\nThis still in Line1')</script>";

Upvotes: 0

Sujit Agarwal
Sujit Agarwal

Reputation: 12508

echo "var users = $(\"#add\").val().split(\"\\n\");";

Upvotes: 0

Adam
Adam

Reputation: 17329

The \n is an escape sequence meaning newline. Backslashes are the beginning of escape sequences, to output a backslash then write \\. So you want \\n. Other useful escape sequences include the quote: use \" to put a quote into the string instead of ending the string.

Upvotes: 5

Related Questions