Solved

Textarea Multi-Line Output from Database Required

Posted on 2004-10-05
20
326 Views
Last Modified: 2013-12-12
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?
0
Comment
Question by:ankuratvb
  • 9
  • 6
  • 3
  • +1
20 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12228123
use nl2br() function

it will turn new lines into <br /> so the html formatting will replace new lines
0
 
LVL 27

Assisted Solution

by:Diablo84
Diablo84 earned 450 total points
ID: 12228137
http://www.php.net/manual/en/function.nl2br.php

for example if your text was stored in $var

echo nl2br($var);
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12228146
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
 
LVL 9

Author Comment

by:ankuratvb
ID: 12229168
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
 
LVL 27

Accepted Solution

by:
Diablo84 earned 450 total points
ID: 12229190
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
 
LVL 9

Author Comment

by:ankuratvb
ID: 12229226
>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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12229254
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
 
LVL 5

Assisted Solution

by:php-webdesign
php-webdesign earned 50 total points
ID: 12232029
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
 
LVL 12

Expert Comment

by:minichicken
ID: 12235401
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Expert Comment

by:php-webdesign
ID: 12564785
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12565118
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
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12565141
yeah sorry diablo, oversaw your post!.. i agree to you
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12565176
:) 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
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12565184
let's just scrape IE... and all go to Firefox :)
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12565218
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
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12565228
hehe :-) i agree... but let's not get tooooo offtopic :)
0
 
LVL 9

Author Comment

by:ankuratvb
ID: 12569155
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 12569186
hi ankuratvb,

Thanks for getting this question closed.

Best Wishes
0
 
LVL 5

Expert Comment

by:php-webdesign
ID: 12571077
yeah thanks :) maybe the style sheet comes in handy someday when IE is gone :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now