How do you create a new line in a textarea when inserting the text? [closed]
Asked Answered
S

11

26

How do you create a new line in a textarea when inserting the text via PHP?

I thought it was \n but that gets literally printed in the textarea.

Subsistence answered 12/10, 2010 at 2:24 Comment(9)
Show us your code, I'm betting you're doing something like echo '\n'; instead of echo "\n";.Certainty
actually im doing echo $contact->address; To get it to work I need to do str_replace('\n', "\n", $contact->address) which is retarded. Why is that?Subsistence
How are you storing the data. Something is getting lost in translation. Do you add_slashes? Save to a database?Freely
@Hailwood. You must have a literal '\n' then. That is, two characters, a backslash and an en, not a newline character (which is represented as "\n" for convenience, but it's really just one char; to use that syntax you must use double quotes; same goes for tabs, for instance, \t).Renter
How are you getting the \n, are you typing it as two distinct characters, or pressing the enter key? More precisely, what's the origin of your data?Certainty
@Subsistence downvoted because the accepted answer is wrong. You should not accept wrong answers. In SO style it's better to write your own and accept it than just accept a wrong answer which will mislead future readers.Speciality
Ok then someone can give a proper answer, But basically, The data is entered in a textarea, hence the new line character comes from pressing enter. The data is then stored in a database, retrieved and put in the textarea.Subsistence
@Subsistence the system is a bit perverse! :) Now my downvote is locked until you edit your question. Please just add a comma or something so that I can upvote it. Thank you. As you were the one to find the actual solution (with help from comments) I would suggest you answer your own question and then accept it in a couple of days.Speciality
Cheers, I am not really answering my own question though, as I still want to know why I need to do that in the first place considering it is coming from a variable, no quotes involved.Subsistence
M
83

Without seeing your code I cannot be sure, but my guess is you are using single quotes ('\n') instead of double quotes ("\n").

PHP will only evaluate escape sequences if the string is enclosed in double quotes. If you use '\n', PHP will just take that as a literal string. If you use "\n", PHP will parse the string for variables and escape sequences and print a new line like you are expecting.

Madelaine answered 13/10, 2010 at 15:34 Comment(1)
And what If you are using no quotes ;) (i.e its coming straight from a variable.Subsistence
Q
10

Try

$text = 'text line one' . PHP_EOL . 'text line two';
echo '<textarea>' . $text . '</textarea>';

Will add each text on reparate line in texarea.

Qumran answered 6/9, 2012 at 12:0 Comment(0)
K
6

PHP Side: from Textarea string to PHP string

$newList = ereg_replace( "\n",'|', $_POST['theTextareaContents']);

PHP Side: PHP string back to TextArea string:

$list = str_replace('|', '&#13;&#10;', $r['db_field_name']);
Kickshaw answered 6/2, 2014 at 20:10 Comment(1)
Warning ereg_replace was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0. Alternatives to this function include: preg_replace()Zygospore
W
3

What Alay Geleynse said was right, I had the same problem as you and the issue was due to the escape characters (\r, \n) was there. To 'unescaped' the variable I used $var = stripcslashes($var) and it's shown correctly

Wallie answered 14/11, 2014 at 5:15 Comment(0)
P
0

Carriage Return

\n 
\r
<br />
^M
Packer answered 13/10, 2010 at 15:40 Comment(0)
M
0

Just to complete the solution for that issue, as others mentioned use "\n" *(or in some cases "\r\n") instead of '\n'

And when you want to show the textarea contents using php and keeping new lines, you need to use nl2br, so if you assing a varialbe for your text and call it $YOUR_TEXT , you should use that function as: nl2br($YOUR_TEXT)

More details could be found here


  • Unix and Unix programs usually only needs a new line \n, while Windows and Windows programs usually need \r\n.
Midmost answered 4/9, 2020 at 12:14 Comment(0)
W
0

I had a similar issue and the solution to mine was double-escaping the newline character. \\n

Wove answered 15/1, 2022 at 11:17 Comment(0)
B
-1

i have used \p for text files. try

Bayern answered 12/10, 2010 at 2:29 Comment(0)
E
-1
//Some Additional Related Validation
function test_input_hacks($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        $data = strip_tags($data);
        return $data;
}

$message = test_input_hacks($_POST['message']);

//For All Operating Systems
echo str_replace(array("\r\n", "\r", "\n"),'<br>', $message);
Eniwetok answered 8/8, 2023 at 21:43 Comment(0)
A
-2
$row['content']=stripslashes($row['content']);
$row['content']=str_replace('<br />',"newline",$row['content']);
$row['content']=htmlentities($row['content']);
$row['content']=str_replace('newline',"<br>",$row['content']);
Altonaltona answered 23/6, 2011 at 2:42 Comment(1)
Welcome to SO jim. Please add a little explanation to your answers pleaseCarpophore
P
-5

Use like this for dynamically enter each line you can use

echo chr(13)
Pumpernickel answered 8/10, 2012 at 10:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.