Log in

View Full Version : Adding Line breaks to a textarea box



smithster
02-24-2011, 01:01 PM
I have tried preg_replace and str_replace but both show \n and \r within the textbox. I want to pull some data out of the database and display it in the textarea to allow for easy update.

The data within one mysql field is seperated by a comma like so...

data1,data2,data3....

I want to replace the comma with a line break like so....

data1
data2
data3....

I have searched and searched but my search ends up with answers to the opposite! Removing line breaks for form submission. I can do that part!

Hope someone can help!

Regards

Smithster

fastsol1
02-25-2011, 02:25 AM
What exactly did you try?Does this work for you?
preg_replace("/\,/", "<br/>", $data)

traq
02-25-2011, 04:08 AM
inside the text box, <br> would be displayed literally. \n, however, should do the trick - I tested this and it worked perfectly:

print '<textarea>line one'."\n".'line two</textarea>';incidentially, this also worked:
print '<textarea>one line
two line</textarea>';though, obviously, you'll be using the newlines when you pull stuff from your database.



I just caught what your problem really was; namely, your \ns and \rs weren't being parsed by php (they were treated as text instead).
Try this (note the double quotes):

$data = 'line one,line two,line three';
$ndata = implode("\n",explode(',',$data));
print "<textarea>$ndata</textarea>";



actually, that's all there was to it. this work fine too:

$data = 'line one,line two,line three';
$ndata = str_replace(',',"\n",$data);
print "<textarea>$ndata</textarea>";
"Double Quotes FTW"