Textarea:按Enter键创建新行

时间:2012-05-01 19:33:11

标签: php textarea

当我在textarea中按Enter键时,它会转到一个新行,但是当提交textarea时,新行不会显示。我该如何工作?它应该很简单,但是从我搜索和尝试的内容中失败了。

<textarea style="{ white-space:pre; }"; name="texts" rows="3" cols="40"></textarea>

$texts = $_POST['texts'];

2 个答案:

答案 0 :(得分:4)

我认为你正在寻找nl2br()功能。

nl2br()

答案 1 :(得分:1)

问题可能是新行显示,但你输出的是html,所以它不会保留新行。当您输出为html时,需要将换行符转换为break标记。你可以这样做:

//editing per comment advice. Apparently this method shouldn't be used since it doesn't replace all possible newline representations, although I dont remember having an issue with it.
//$string = str_replace("\n", "<br />", $string);
//as others mentioned, this is better and easier:
$string = nl2br($string);

也许我误解了这个问题,但这就是我从中得到的。