[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

echo new line in textarea

If I type:
1
2
3
into the first textarea box and submit it to a second textarea box, I get:
1<br />
2<br />
3
while using this to process it: $output = str_replace("\n", '', nl2br($input));


I just want the second box to be the same as the first. How do I do this?
Like this:
1
2
3
0
ray-solomon
Asked:
ray-solomon
  • 3
  • 2
1 Solution
 
TeRReFCommented:
Don't use this line!
$output = str_replace("\n", '', nl2br($input));
It replaces \n (which is a newline) to <br />
0
 
TeRReFCommented:
Just use:
$output = $input;
0
 
ray-solomonAuthor Commented:
Sorry, let me refine my question. I saw you posted to faster than me editing this question.

I am trying to make the output look like this:
1
2
3
"1"
"2"
"3"
[1]
[2]
[3]
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ray-solomonAuthor Commented:

I tried using this, but it does not work properly.

echo $output."\n"."&#34;".$output."&#34;"."\n"."&#91;".$output."&#93;";

result:
1
2
3
"1
2
3"
[1
2
3]
0
 
ray-solomonAuthor Commented:
Here is how I am getting the form values:

  foreach ($_POST as $key => $value) {
  $kw = $$key = trim($value);
  }

if (isset($_POST['field1'])) { $output = $kw; }
0
 
AutogardCommented:
 $string = "Hey there\r\nHow are you\r\ntoday?";

  $output1 = $string;
  $output2 = "\"" . str_replace("\r\n", "\"\r\n\"", $string) . "\"";
  $output3 = "[" . str_replace("\r\n", "]\r\n[", $string) . "]";

  print "<textarea>";
  print "$output1";
  print "</textarea>";

  print "<textarea>";
  print "$output2";
  print "</textarea>";

  print "<textarea>";
  print "$output3";
  print "</textarea>";

yields:

Hey there
How are you
today?

"Hey there"
"How are you"
"today?"

[Hey there]
[How are you]
[today?]

(but in textareas of course)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now