-2

I got a little problem here.

The thing is that in my database I got a text, and then I print it onto the screen and the code goes like:

<p>Text 1</p>
<p>Text 2</p>

Thats the output in HTML cause I use tinyMCE plugin. Goes well so far. The thing is, that I want to put it into a javascript variable (like: onclick="my_function(the_text_above)"), but the output code makes a linebreak and that goes into an illegal token when the function runs.

Hope you can lend me a hand with this issue I encountered.

scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
Ardilla
  • 51
  • 1
  • 2
  • 10

3 Answers3

1

You just replace /n (new line) with nothing

PHP

echo str_replace("\n","","your \n text");

Javascript

"Your \ntext".split("\n").join("")
  • thanks you too, used the preg_replace that I found in the possible duplication of post that can be found at the top. thanks for all – Ardilla Dec 05 '13 at 23:34
0

Use json_encode for that.

It will look like:

var jsVariable = <?php echo json_encode($stringFromDb); ?>;

json_encode will produce legal js literal regardless of input.

PS: it makes no sense to remove lines if what you need is just to sanitize the data properly.

zerkms
  • 249,484
  • 69
  • 436
  • 539
0

I understand you want to strip the line breaks from your string. You can use str_replace to do that.

str_replace("\n",'');

this will remove UNIX breaks from your string. I don't know which type of break your code contain, so \n might not work right away. another one is "\r" and I think there's few others, I sadly don't know them all.