tags:

views:

45

answers:

2

How can I read the New-line so that when users fill in a text-area inside my form, and then hit submit, the text is displayed exactly the same way as they wrote it?

I should mention I read in the variables from the form with POST method in my php file.

Thanks

+2  A: 

Take a look at the nl2br function.

Example from documentation:

enter code here

<?php echo nl2br("foo isn't\n bar"); ?>

The above example will output:

foo isn't<br /> bar

Chris Pebble
+4  A: 

The newline character in PHP is presented by the string "\n".

If you want to reproduce the newlines of your users input in HTML, you have to convert every \n to a <br />. You could do that by hand or use the function nl2br in such a way:

echo nl2br($_POST['input']);
Matze
don’t forget to sanitize `$_POST['input']` before with something like `htmlspecialchars`
knittl