php :: new line in textarea?

℡╲_俬逩灬. 提交于 2019-11-27 02:11:37

问题


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


回答1:


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.




回答2:


Try

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

Will add each text on reparate line in texarea.




回答3:


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



回答4:


Carriage Return

\n 
\r
<br />
^M



回答5:


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




回答6:


i have used \p for text files. try




回答7:


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



回答8:


Use like this for dynamically enter each line you can use

echo chr(13)


来源:https://stackoverflow.com/questions/3911261/php-new-line-in-textarea

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!