dotJoon
December 20, 2010, 1:19am
1
<?php
if ( isset($_POST['myForm']) )
{
$myForm=$_POST['myForm'];
[COLOR="Red"]echo $myForm;[/COLOR]
}
else
{$myForm="";}
?>
<form action="myForm.php" method="post">
<textArea name="myForm"><?php echo $myForm ?></textArea>
<input type='submit'>
</form>
When I make line breaks in the textArea form box, the line breaks isn’t showed in the result of “echo $myForm”?
How can I make line breaks in “echo $myForm” in responding to making line breaks in the textArea form box?
I tried the code below but it seems not to work.
$myForm=str_ireplace("$chr(13)","[COLOR="Red"]<br>[/COLOR]",$myForm);
JREAM
December 20, 2010, 1:51am
2
The textarea reads literally, meaning your text has to look like this:
some text here,
and this is a new line,
another real new line
I think this should happen automatically and you don’t need to use <br /> tags, rather PHP automatically inserts \r or
lines.
The code from your post would look like:
some text here,\
and this is a new line,\
another real new line
If it doesn’t look that way, please tell us
dotJoon
December 20, 2010, 2:40am
3
JREAM:
The textarea reads literally, meaning your text has to look like this:
some text here,
and this is a new line,
another real new line
I think this should happen automatically and you don’t need to use <br /> tags, rather PHP automatically inserts \r or
lines.
The code from your post would look like:
some text here,\
and this is a new line,\
another real new line
If it doesn’t look that way, please tell us
The code I posted is in http://dot.kr/x-test/myForm1.php .
When I submit the value of textArea which has some line breaks,
It doesn’t show any
or \r or line breaks in echoing.
dotJoon
December 20, 2010, 3:13am
5
I change from the code above to the code below.
<?php
if ( isset($_POST['myForm']) )
{
$myForm=$_POST['myForm'];
echo [COLOR="Red"]nl2br[/COLOR]($myForm, false);
}
else
{$myForm="";}
?>
<form action="myForm2.php" method="post">
<textArea name="myForm"><?php echo $myForm ?></textArea>
<input type='submit'>
</form>
When I submit the form in http://dot.kr/x-test/myForm2.php , it says “Warning : Wrong parameter count for nl2br() in myForm2.php on line 7”.
How can I fix it?
system
December 20, 2010, 3:36am
6
your code works fine on my local server.
but try taking out the false paramater in nl2br
echo nl2br($myForm);
dotJoon
December 20, 2010, 4:03am
7
It works fine at first, but after I submit the textArea data with some line breaks, it says the warning.
The code below works fine without any warnings. but it doesn’t display any line breaks in echoing the submitted data when I submit the textArea data with some line breaks.
<?php
if ( isset($_POST['myForm']) )
{
$myForm=$_POST['myForm'];
[COLOR="Red"]echo nl2br($myForm);[/COLOR]
}
else
{$myForm="";}
?>
<form action="myForm2.php" method="post">
<textArea name="myForm"><?php echo $myForm ?></textArea>
<input type='submit'>
</form>
The code you have posted here works at my end as well. Can you post/give us the sample code that you entered in your textarea?
dotJoon
December 20, 2010, 4:37am
9
http://dot.kr/x-test/myForm2.php for the code below.
<?php
if ( isset($_POST['myForm']) )
{
$myForm=$_POST['myForm'];
echo [COLOR="Red"]nl2br($myForm, false);[/COLOR]
}
else
{$myForm="";}
?>
<form action="myForm2.php" method="post">
<textArea name="myForm"><?php echo $myForm ?></textArea>
<input type='submit'>
</form>
http://dot.kr/x-test/myForm3.php for the code below.
<?php
if ( isset($_POST['myForm']) )
{
$myForm=$_POST['myForm'];
echo [COLOR="Red"]nl2br($myForm);[/COLOR]
}
else
{$myForm="";}
?>
<form action="myForm3.php" method="post">
<textArea name="myForm"><?php echo $myForm ?></textArea>
<input type='submit'>
</form>
Changelog
Version Description
5.3.0 Added the optional is_xhtml parameter.
4.0.5 nl2br() is now XHTML compliant. All older versions will return string with ‘<br>’ inserted before newlines instead of ‘<br />’.
I am sure you are using PHP version less than 5.3 because the second parameter is_xhtml was added only in 5.3.