Php text区域:按enter键创建新行

Php text区域:按enter键创建新行,php,textarea,Php,Textarea,当我在textarea中按enter键时,它将转到一个新行,但在提交textarea时,该新行不会显示。我该怎么做?这应该很简单,但从我的搜索和尝试来看,失败了 <textarea style="{ white-space:pre; }"; name="texts" rows="3" cols="40"></textarea> $texts = $_POST['texts']; $text=$_POST['text']; 问题可能是显示了新行,但您将其输出为html

当我在textarea中按enter键时,它将转到一个新行,但在提交textarea时,该新行不会显示。我该怎么做?这应该很简单,但从我的搜索和尝试来看,失败了

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

$texts = $_POST['texts'];

$text=$_POST['text'];

问题可能是显示了新行,但您将其输出为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);
//编辑每条注释建议。显然,这个方法不应该被使用,因为它并没有替换所有可能的换行符表示,尽管我不记得它有问题。
//$string=str\u replace(“\n”,“
,$string”); //正如其他人所提到的,这样做更好、更容易: $string=nl2br($string);

也许我误解了这个问题,但这是我从中得出的结论。

我想您正在寻找
nl2br()
函数


请发布相关代码。我更喜欢使用PHP使其工作,但任何使其工作的方法都可以。新行字符是空白字符,因此您不必在简单的
var\u dump
echo
中看到它们。您是否对文本进行过任何编辑?可以删除“\r\n”符号的内容?在这种情况下,可能会首选。编辑:不是投票人。。这个答案绝对有用:)足够公平:)有什么区别,nl2br转换\r\n以及?编辑:nvm,阅读文档。