1

I have a strange issue here, can anyone tell me why my nl2br does not seem to be achieving the desired effect?

Here's the screenshot of the data as stored in mysql phpmyadmin: enter image description here

Here's a screenshot of my source code jquery / php code to display the data in a text area: enter image description here

Here's a screenshot of the resulting jquery code which results in a Uncaught SyntaxError: Unexpected token ILLEGAL error, i suspect its because of the carriage return in the actual source code. enter image description here

I've tried it without nl2br, all it does is create the same output (website source code) but without the HTML break tag. Both ways it seems to create the "carriage return " in the javascript code itself.

What am i missing here? Something missing with my source data or code?

aDvo
  • 894
  • 4
  • 15
  • 43
  • possible duplicate of [How to remove line breaks (no characters!) from the string?](http://stackoverflow.com/questions/10757671/how-to-remove-line-breaks-no-characters-from-the-string) – kenorb Mar 03 '15 at 00:29

2 Answers2

3

nl2br — Inserts HTML line breaks before all newlines in a string

It doesn't replace newlines, it just adds a line break. If you need to remove them, you have to use something like str_replace

str_replace( "\n", '<br />', $text );

Or to make sure you get all variants of a newline:

str_replace(array("\r\n", "\r", "\n"), "<br />", $text); 
dave
  • 62,300
  • 5
  • 72
  • 93
0

This worked for me: Pass a PHP string to a JavaScript variable (and escape newlines)

Which tells us to json_decode($phpvariable) to make it javascript friendly

Community
  • 1
  • 1
aDvo
  • 894
  • 4
  • 15
  • 43