Hailwood
Hailwood

Reputation: 92581

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

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.

Upvotes: 26

Views: 70468

Answers (11)

Emad Morris Zedan
Emad Morris Zedan

Reputation: 301

//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);

Upvotes: -1

LewyT
LewyT

Reputation: 1

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

Upvotes: 0

usama sulaiman
usama sulaiman

Reputation: 2021

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.

Upvotes: 0

Va1iant
Va1iant

Reputation: 223

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

Upvotes: 3

cssyphus
cssyphus

Reputation: 40038

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']);

Upvotes: 6

Devaraj
Devaraj

Reputation: 1

Use like this for dynamically enter each line you can use

echo chr(13)

Upvotes: -5

OzzyCzech
OzzyCzech

Reputation: 10342

Try

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

Will add each text on reparate line in texarea.

Upvotes: 10

jim
jim

Reputation: 1

$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']);

Upvotes: -2

Phill Pafford
Phill Pafford

Reputation: 85298

Carriage Return

\n 
\r
<br />
^M

Upvotes: 0

Alan Geleynse
Alan Geleynse

Reputation: 25139

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.

Upvotes: 83

zod
zod

Reputation: 12417

i have used \p for text files. try

Upvotes: -1

Related Questions