简体   繁体   中英

php :: new line in textarea?

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.

Thanks

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.

Try

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

Will add each text on reparate line in texarea.

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

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

Carriage Return

\n 
\r
<br />
^M

i have used \\p for text files. try

2020 - Google brought me here for something similar.

Building on answer from user @Va1iant above

Environment:

  • HTML 5 / Bootstrap 4.5
  • php 7.3.x
  • mariadb 10.2.x

Problem :

  • Input Form with textarea ( no issue of \\r\\n appearing in database after posting )
  • Edit form with textarea ( \\r\\n appears in db and also in the edit form after posting )

Solution:

  • DB - column type for the textarea content set to 'varchar'
  • Edit Form - code snippet for content inside textarea( stripcslashes used ) shown below
<?= isset($_POST['MailingAddress']) ? stripcslashes(misc::esc($_POST['MailingAddress'])) : misc::esc($data['current_mailing_address']) ; ?>

where misc::esc -> return htmlspecialchars($var, ENT_QUOTES|ENT_HTML5, 'UTF-8');

Note :

I did not experiment with white-space: pre-line; as outlined at CSS Tricks so that could be a solution too - without using stripcslashes.

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 .

if you need only print the data use nl2br() , The nl2br convert \\n to <br /> .but <br /> not create New line. you should repalce <br /> to <br> then this will work.

example:

$content = "foo \n bar";
echo str_replace("<br />","<br>",nl2br($content));

result:

foo
bar

if you need to split it , You can Use my function. :D

function SplitLine($txt){
            $txt  = nl2br($txt);
            $txt = explode("<br />",$txt);
            return $txt;
            
        }

I'm sent foo \\n bar with textarea as POST method to php.

print_r(SplitLine($_POST['text_area']));

my Result :

Array ( [0] => foo [1] => bar)

you can use for loop to get data and print it:

$result = SplitLine($_POST['text_area']);
for($i = 0;$i < count($result);$i++){
   echo $result[$i]."<br>";
}

and result is

foo
bar

full code ( Only For TEST ):

<?php 
//MyFunction
function SplitLine($txt){
    $txt  = nl2br($txt);
    $txt = explode("<br />",$txt);
    return $txt;        
}
//My Code
if(isset($_POST['submit_data'])){
    print_r(SplitLine($_POST['text_area']));
}
?>
<br>
<form method='post'>
    <textarea name='text_area'></textarea>
    <br>
    <input type='submit' value='Send Data :)' name='submit_data' />
</form>

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

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

像这样使用动态输入您可以使用的每一行

echo chr(13)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM