Page 1 of 1

Retain formatting with <textarea>

Posted: Wed Jan 21, 2004 9:41 pm
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?

Posted: Wed Jan 21, 2004 10:51 pm
by microthick
You can use the function nl2br() to convert new lines from the text area into HTML <br>'s.

Posted: Thu Jan 22, 2004 3:39 am
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

Posted: Thu Jan 22, 2004 3:43 am
by patrikG
Or use [php_man]wordwrap[/php_man].

Posted: Thu Jan 22, 2004 5:59 am
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

Posted: Thu Jan 22, 2004 7:33 am
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?

Posted: Thu Jan 22, 2004 7:39 am
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.

Posted: Thu Jan 22, 2004 7:49 am
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]

Posted: Thu Jan 22, 2004 8:10 am
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?

Posted: Thu Jan 22, 2004 8:19 am
by malcolmboston
wordwrap is useless regarding formatting really

Posted: Thu Jan 22, 2004 8:23 am
by patrikG
Read Twig's post above - that should clarify things.

Posted: Thu Jan 22, 2004 8:24 am
by malcolmboston
ooooooooooh

thank you! it works :lol:

Thanks!

Posted: Thu Jan 22, 2004 4:07 pm
by coindood
Man! I had just glanced over it! Thanks a lot! Thank you AND Twig!!!!!! :D