Keep formatting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ace2600
Forum Commoner
Posts: 30
Joined: Fri Jun 21, 2002 12:12 am

Keep formatting

Post by ace2600 »

I have a multiline text field in a form thats information is sent to my db. When a user views a page that outputs the db with echo "$multiline"; the line breaks the user originally put in are not there.
Is this because the db only keeps the text not formatting or because the echo command doesnt preserve the formatting?
More importantly, how do I display the info the user typed, how he typed it?
Thank you,
Ace
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

check nl2br
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

If I input:
Bla

Bla bla

bla
into a textbox named 'bob'.

So you usually see: "BlaBla blabla" when u echo $bob. In the database, it is multiline....but yeah..it doesn't preserve it...

TO DO THIS: suppose you extract the contents of what you want from ur database and u put it into a variable called '$bob'

Code: Select all

$search = array("/\n/");
$replace = array("<br />\n");
$multilinebob = preg_replace($search, $replace, $bob);
then when you echo $multilinebob, you get a multiline version....you could ASLO do this with spaces if you want...make it replace ' ' with '&nbsp;'

hope i helped....

later on, -Brian
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Never knew there was a function called "nl2br". It's realy usefull :!: Thanks
Post Reply