Textarea Multi-Line Output from Database Required

Hi,

I have a form in which i have a textarea.On Submitting the form,I save the contents of the textarea to a database.

When i retrieve it from the database,if i display it as normal text using echo,all of the output comes on a single line,with \n etc. all treated as spaces.

IS there any way i can get the multi-line output as in the textarea ,using normal echo or on the page as normal text?

If so,how?
LVL 9
ankuratvbAsked:
Who is Participating?
 
Diablo84Connect With a Mentor Commented:
hmm nl2br should work fine, not sure of the cause of the error, in the mean time try this instead

$var = str_replace("\n","",$var);
echo $var;

Incidently, >>Does anyone know how to change the font in pre-formatted text?

I don't believe that is possible
0
 
Diablo84Commented:
use nl2br() function

it will turn new lines into <br /> so the html formatting will replace new lines
0
 
Diablo84Connect With a Mentor Commented:
http://www.php.net/manual/en/function.nl2br.php

for example if your text was stored in $var

echo nl2br($var);
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
Diablo84Commented:
you could also use the html pre tags as well to maintain formatting

again, if your text was stored in $var

echo "<pre>\n";
echo $var;
echo "</pre>\n";
0
 
ankuratvbAuthor Commented:
Thanx Guys.

nl2br() didnt work.It Says CGI-Application Error.Do i need to include something before using
the function.

Pre-formatted text <pre> tags worked.Does anyone know how to change the font in
pre-formatted text?

Also,i'm having another problem with SESSIONS.Pls see my other thread.
0
 
ankuratvbAuthor Commented:
>I don't believe that is possible

Thats why i was still looking for an alternate solution.

Sorry,cant test this right now,will give it a try first thing tomorrow morning.
BTW
>$var = str_replace("\n","",$var);

replaces all \n with a blank string.Shouldnt that have been

$var = str_replace("\n","<br>",$var);
0
 
Diablo84Commented:
sorry, yes that is what i meant.

I was trying to find the reason for the error caused by nl2br (haven't seen this come up before) so was distracted while typing/
0
 
php-webdesignConnect With a Mentor Commented:
change the font can maybe be done like this:

remove the <pre>

add:

<div class="likePre">
Tis is
pre-formated text
</div>

and a css style:

.likePre {
white-space:pre;
font-family: sans-serif;
}

http://www.w3schools.com/css/css_font.asp

this is CSS2, and will not work on IE versions prior to 6.. will work ok on netscape and firefox etc
0
 
minichickenCommented:
You can also do this when you echo the ouput

echo str_replace(chr(13), "<br>", $your_string); //chr(13) is the ASCII for "ENTER"
0
 
php-webdesignCommented:
me and minichicken gave the last 2 responses and did not get any feedback if they worked (or not) so i think a split points will be fair
0
 
Diablo84Commented:
php-webdesign whitespace is not a reliable method as it does not work in IE, a major problem as IE is still the browser of choice for many internet users.

The use of nl2br should have been fine however as that failed on this occasion the str replacement of \n should have worked fine. Minichicken's example of using chr(13) instead of \n would render the exact same output and relys on the same function therefore my recommendation is:

Accept Diablo84 {12229190}
0
 
php-webdesignCommented:
yeah sorry diablo, oversaw your post!.. i agree to you
0
 
Diablo84Commented:
:) Unfortunate that whitespace is restricted to Mozilla/Netscape, it would otherwise be a very useful attribute. I think the same can be said for min-width/max-width etc. One day IE will stop giving developers headaches.

0
 
php-webdesignCommented:
let's just scrape IE... and all go to Firefox :)
0
 
Diablo84Commented:
I agree :)

I have been tempted for some time to just forget IE users completely:

if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']),"msie") die("Please Come Back When you have a Competent Browser");
0
 
php-webdesignCommented:
hehe :-) i agree... but let's not get tooooo offtopic :)
0
 
ankuratvbAuthor Commented:
Hi,

Sorry for the delay in closing these questions.

I was off the boards for quite a while.

I'll split points b/w Diablo84(2 comments -nl2br and str_replace) 450 pts and some for php-webdesign 50 pts. for the style-sheet info. even if thats not very useful right now.

Thanx.

0
 
Diablo84Commented:
hi ankuratvb,

Thanks for getting this question closed.

Best Wishes
0
 
php-webdesignCommented:
yeah thanks :) maybe the style sheet comes in handy someday when IE is gone :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.