Page 1 of 1

how to insert carriage return

Posted: Thu Nov 04, 2004 11:28 am
by briwal222
I have a text box, and I would like to add in a carriage return in the box when inputting data through a php script, so that when the information is seen on the website, it would be formatted thus. Can anyone point me in the direction of a script that would help to make this possible?

Posted: Thu Nov 04, 2004 12:51 pm
by rehfeld
$text = "line one\r\nline two\r\nline three\r\n";

<textarea><?php echo $text; ?></textarea>



carriage return is accomplished by using "\r" (must be inside double quotes)
new line is accomplished by using "\n" (must be inside double quotes)

macs use \r for a "new line", *nix use \n, win uses \r\n

you should use \r\n, that way you know its always gonna work

Posted: Thu Nov 04, 2004 12:56 pm
by timvw
probably [php_man]nl2br[/php_man] is an interesting function :)

Posted: Thu Nov 04, 2004 1:15 pm
by kendall
Arent carriage return invisibly inserted in wrapped text areas?

KEndall

Posted: Thu Nov 04, 2004 1:19 pm
by briwal222
OK I think I sort of understand. Does this mean that on the webform that I type into, that if I were to type in: "This restaurant is great. \r We love to eat here."
That it would return this:

This restaurant is great.
We love to eat here.

Thanks again.

Posted: Thu Nov 04, 2004 1:34 pm
by rehfeld
yes, but ONLY if you have have php echo it


when php encounters a \r inside of a double quotted string, it will change \r into a carriage return


try the little code bit i posted


again, id recomend using \r\n, because if the browser doesnt interpret a lone \r or \n correctly, visually to the user it will look like its all on 1 line.
if you use \r\n then all browsers will definately render it correct.

theres prob few browsers that wouldnt interpret a lone \r or \n correctly, but i dont see any real drabacks to being safe about it.

Posted: Thu Nov 04, 2004 1:38 pm
by rehfeld
kendall wrote:Arent carriage return invisibly inserted in wrapped text areas?

KEndall

depending on the browser, and if the html has specified a certain wrap="virtual" attribute or whatever, yes. (default most browsers definately insert temporary, line breaks)

but when they submit the form, the virtual line breaks wont be sent. but depending on the wrap="" it could be. theres diff ways you can choose as the html designer.

i hope i have confused you :)