Page 1 of 1

return key should create a <br> in text area

Posted: Thu Nov 11, 2004 9:17 pm
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

Posted: Thu Nov 11, 2004 9:56 pm
by scorphus
[php_man]nl2br[/php_man]()

Posted: Thu Nov 11, 2004 10:26 pm
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?

Posted: Thu Nov 11, 2004 10:38 pm
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