Text losing whitespace between MySQL and Textarea

Posted 3 years ago by robjbrain

I have a textarea that a user can fill in, much like the textarea on this forum. When I try to repopulate that textarea from the database for editing, multiple new lines are lost.

The database shows this:

[b]some text[/b]\r\n
A: More text\r\n
\r\n
[b]other text[/b]\r\n
A: More other text\r\n

When I put it back in a textarea input it still has new lines, but double new lines are lost:

[b]some text[/b]
A: More text
[b]other text[/b]
A: More other text

I have tried this without using the blade escape syntax (just a plain echo) with the same result.

Infact I can't tell when it is losing the whitepace at all. Here are some examples of when I can see the string having multiple whitespace and when I can't.

dd($model);
// This shows with whitespace (/r/n) in both the attributes and originals array
dd($model->column);
// This shows the multiple new lines \r\n
var_dump($model->column);
// This shows no new lines
return $model->column;
// simple return from the controller removes multiple white lines 
return Response::make($model->column);
// again just outputting the plain string, shows multiple white lines removed
<textarea>{!! $model->column !!}</textarea>
// of course the actual problem in a blade file, no extra spaces between lines are displayed in the textarea

What's more confusing is that in vagrant the whitelines are retained just fine, only on my centos server are they lost. The code is identical which made me think it might be a mysql/php/server setting, but then dd() on the model shows the correct whitespace so it's obviously coming out of the database just fine, something is happening to the string when it's echo'd or var_dump'd (i'm not sure what dd() uses I assumed it was var_dump, perhaps not).

It does seem to be related to the database in some way though, because a form with a validation error repopulates the textarea just fine with the multiple new lines. It's only when repopulating the textarea from the database that a problem occurs.

Every result i've found trying to google this error references using nl2br, which obviously isn't helpful in this instance. I'm not trying to display the text in html, i'm trying to let a user submit a form using the textarea and then refill that textarea to let them edit the data they've submitted later on. So to be very clear, nl2br is not a solution in this instance.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.