文本区域:按回车键创建新行


Textarea: pressing enter creates new line

当我在文本区域中按回车键时,它会转到一个新行,但是在提交文本区域时,该新行没有显示。我该如何完成这项工作?它应该很简单,但从我搜索和尝试的结果来看失败了。

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

我认为您正在寻找nl2br()函数。

nl2br()

问题可能是新行显示,但您将其输出为 html,因此它不会保留该新行。输出为 html 时,需要转换换行符以断开标记。你可以这样做:

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

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