Retain formatting with <textarea>

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
coindood
Forum Newbie
Posts: 11
Joined: Mon Jan 19, 2004 2:01 pm

Retain formatting with <textarea>

Post by coindood »

I have a text area form object and whenever I save the contents onto mysql, none of the formatting is saved is there a way I van retain formatting like newlines and such without having to actually use html code within the post?
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

You can use the function nl2br() to convert new lines from the text area into HTML <br>'s.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

But don't use nl2br() before putting the data into the database, only use it when you want to redisplay that data - the formatting is saved in the database, outputting the data into <pre> tags will show you this, and you really don't want to be storing lots of <br />'s.

Mac
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Or use [php_man]wordwrap[/php_man].
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

this is something i picked up on a few days ago as i was writing articles into mysql and it was'nt taking any notice of my line breaks etc

thank you very much
coindood
Forum Newbie
Posts: 11
Joined: Mon Jan 19, 2004 2:01 pm

Post by coindood »

how would I integrate the nl2br() function within the <textarea> tag? I saw something about it as a post on the function reference page but I I really don't know if it was what I needed. the post was by of:
jhfh at hetnet dot nl
I also tried looking through the source of the thread posting page and found a js function called storeCaret(this);. is this important?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Use [php_man]wordwrap[/php_man].

Example (from the manual):

Code: Select all

Wraps a string to a given number of characters using a string break character. (PHP 4 >= 4.0.2) 
string wordwrap ( string str [, int width [, string break [, boolean cut]]] )

Returns a string with str wrapped at the column number specified by the optional width parameter. The line is broken using the (optional) break parameter. 

wordwrap() will automatically wrap at column 75 and break using '\n' (newline) if width or break are not given. 

If the cut is set to 1, the string is always wrapped at the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example). 

Note: 
The optional cut parameter was added in PHP 4.0.3 

Example 979. wordwrap() example
<?php 
$text = "The quick brown fox jumped over the lazy dog."; 
$newtext = wordwrap($text, 20, "<br />"); 

echo "$newtext\n"; 
?>

This example would display: 

The quick brown fox 
jumped over the 
lazy dog.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

so then

i have a personal comments / about me textarea on my site (longtext format in MySQL) now even though i have pressed return when filling out the form so in the text area is looks like this
I am a web-designer

I am also learning PHP
it does this
i am a web-designer i am also learning PHP
should i create a replace string?[/quote]
coindood
Forum Newbie
Posts: 11
Joined: Mon Jan 19, 2004 2:01 pm

Post by coindood »

That's exactly my problem. I don't want to word wrap at a certain point or type in newlines everywhere, I want to retain what formatting was inputted into the textarea, so if I made a list like:
1.
2.
3.

It would come out exactly like that instead of:
1.2.3.

But I will remember the other functions in future problems needing them. thanks!

P.S.
Does anyone hava an exmple of this with source?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

wordwrap is useless regarding formatting really
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Read Twig's post above - that should clarify things.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

ooooooooooh

thank you! it works :lol:
coindood
Forum Newbie
Posts: 11
Joined: Mon Jan 19, 2004 2:01 pm

Thanks!

Post by coindood »

Man! I had just glanced over it! Thanks a lot! Thank you AND Twig!!!!!! :D
Post Reply