This question already has an answer here:
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.
</div>
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.
I understand you want to strip the line breaks from your string. You can use str_replace to do that.
str_replace("
",'');
this will remove UNIX breaks from your string. I don't know which type of break your code contain, so might not work right away. another one is "" and I think there's few others, I sadly don't know them all.
You just replace /n (new line) with nothing
PHP
echo str_replace("
","","your
text");
Javascript
"Your
text".split("
").join("")