Page 1 of 1

Taking PARAGRAPH input from a html form and saving in a PHP

Posted: Sun Mar 13, 2005 11:31 am
by jclarkkent2003
Hey guys,
I'm trying to take a data paragraph input from a form variable named instructions, and then put it into a form where it can be saved in a php variable named $instructions.

I fixed the error when someone inputs something in "Text" and it automatically string replaces the " with \", It would be nice if I knew how to make it so if it was ALREADY \" escaped, then it wouldn't do it again and right now it makes it \\", now I just need to correct the input format so when there are returns, that it inserts a <br> at the END of the line, and then goes to a new line. here is what I have

Code: Select all

<?
if($_POST["instructions"]){ $instructions = $_POST["instructions"]; }

$instructions = str_replace("\n", "\n\n", $instructions);
$instructions1 = str_replace("\n", "<br>\n", $instructions);
$instructions2 = str_replace("\n<br>", "<br>", $instructions1);
$instructions3 = str_replace("\n\n", "\n", $instructions2);
$instructions = str_replace('"', '\"', $instructions3);

?>
When I input this:
Description of the game and more

What are you doing? How'd you like some muffins? Test this "out" you kl....

Test
I get this in the $instructions variable:
Description of the game and more
<br>

<br>
What are you doing? How'd you like some muffins? Test this \"out\" you kl....
<br>

<br>
Test
And what I WANT is this EXACTLY:
Description of the game and more<br><br>
What are you doing? How'd you like some muffins? Test this \"out\" you kl....<br><br>
Test
I want those extra end lines (\n I think?) deleted and it only has a end line at the end of the line totally. Can I get some help?

I have tried things like this:
$instructions = str_replace("\n<br>([\n]{1,5})<br>", "<br><br>\n", $instructions);
But it doesn't work like I was expecting.


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] :grumble-grumble:[/color]

Posted: Sun Mar 13, 2005 12:42 pm
by anjanesh
For displaying \n chars as a newline in html theres a built in function : nl2br - http://www.php.net/manual/en/function.nl2br.php

Posted: Sun Mar 13, 2005 1:25 pm
by jclarkkent2003
ok thank you, that helped condense my code.

I know have this:

$instructions = str_replace("\r", "<br>\r", $instructions);
$instructions = str_replace('"', '\"', $instructions);

which turns:

"test

123

test"

into

"test<br>
<br>
123<br>
<br>
test<br>
<br>"

Now how do I take this:

"<br>
<br>
"

into "<br><br>
"

?

Thanks again.

I have tried things like str_replace("<br>\n<br>\n","<br><br>\n") but that didn't work for some reason. Why not?

Posted: Sun Mar 13, 2005 1:33 pm
by Chris Corbyn
If you're on windows

Code: Select all

&quote;\n&quote; is &quote;\r\n&quote;
:wink:

Posted: Sun Mar 13, 2005 1:44 pm
by anjanesh
d11wtq wrote:If you're on windows "\n" is "\r\n"
But If I open a text file which is terminated by \n in Notepad, it wont be shown as new lines - some small box will show instead of a newline - all in noe straight line (unless its wordwrapped) - I have to use another editor to parse \n as newline also. Windows bundled s/w reads \r\n as newline.

Posted: Sun Mar 13, 2005 1:45 pm
by jclarkkent2003
I'm on linux,

I tried

$instructions = str_replace("<br>\r\n<br>", "<br>", $instructions);

but that didn't even work.

Any more ideas? Is there a difference for linux? I input

-------------------
test

123

test
-----------------------

and got output

-----------------------
test<br>
<br>
123<br>
<br>
test
-----------------------

I just need to get rid of whatever that new line or return is between the two <br> 's.

Posted: Sun Mar 13, 2005 1:57 pm
by anjanesh
Try doing both - in this sequence - first remove \r\n and then remove remaining \n alone

Code: Select all

$instructions = str_replace(&quote;<br>\r\n<br>&quote;, &quote;<br>&quote;, $instructions);
$instructions = str_replace(&quote;<br>\n<br>&quote;, &quote;<br>&quote;, $instructions);

Posted: Sun Mar 13, 2005 2:02 pm
by jclarkkent2003
I tried that and still got

test<br>
<br>
123<br>
<br>
test

Posted: Sun Mar 13, 2005 2:06 pm
by jclarkkent2003
holla!!

Alright,


$instructions = str_replace("<br>\r\n<br>", "<br><br>", $instructions);
$instructions = str_replace("<br>\n<br>", "<br>", $instructions);

worked, just needed an extra adjustment.

Thanks for all you guys help, I was totally lost and didn't remember \r, I was always looking for \n which was my problem.

thanks agian`~!