how to insert carriage return

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
briwal222
Forum Newbie
Posts: 10
Joined: Fri Oct 29, 2004 9:47 am

how to insert carriage return

Post 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?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

probably [php_man]nl2br[/php_man] is an interesting function :)
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post by kendall »

Arent carriage return invisibly inserted in wrapped text areas?

KEndall
briwal222
Forum Newbie
Posts: 10
Joined: Fri Oct 29, 2004 9:47 am

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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 :)
Post Reply