return key should create a <br> in text area

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
sebnewyork
Forum Commoner
Posts: 43
Joined: Wed Mar 17, 2004 10:20 pm

return key should create a <br> in text area

Post by sebnewyork »

Hi all
I made a simple php page with a form with a text area and a submit button. Clicking the submit button writes the content of the text area into an html page.
It is like a very very basic blog, and I'm glad I got so far.
The next thing I need to figure out is:

when I write in the text area, and press the return key to go to the next line, I want this to translate into a <br>, so that the line breaks will be reproduced when the content of the text area is written into the html page.
(oh, and I need something that will work in both Mac and PC).

How can I do that?

Thanks for your help
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

[php_man]nl2br[/php_man]()
sebnewyork
Forum Commoner
Posts: 43
Joined: Wed Mar 17, 2004 10:20 pm

Post by sebnewyork »

Thanks, I did a google search and found this
nl2br()
as well as other ways to do it.
I must be missing something, because nothing worked so far:

I've tried:

Code: Select all

<?php
if (isset ($_POST['submit'])) {
	if (!empty ($_POST['content'])) {
		if ($fp = fopen ("text.htm", 'a')) {
			fwrite ($fp, nl2br("$content"));
			fclose ($fp);
			header ('location: text.htm');
		}
	}
}
?>
I've tried:

Code: Select all

<?php
if (isset ($_POST['submit'])) {
	if (!empty ($_POST['content'])) {
		if ($fp = fopen ("text.htm", 'a')) {
			$data = str_replace("<br>", "\n", $content);
			fwrite ($fp, "$data");
			fclose ($fp);
			header ('location: text.htm');
		}
	}
}
?>
I've tried also with \r\n instead of \n
In all cases the text does get written in "text.htm", but no line breaks.
What am I missing?
sebnewyork
Forum Commoner
Posts: 43
Joined: Wed Mar 17, 2004 10:20 pm

Post by sebnewyork »

sorry

i changed the name of my page and didn't change the form action, that's why it was not working....

problem solved, thanks
Post Reply