簡體   English   中英

Textarea:按Enter鍵創建新行

[英]Textarea: pressing enter creates new line

當我在文本區域中按Enter鍵時,它會轉到新行,但是在提交文本區域時,該新行不會顯示。 我該如何工作? 它應該很簡單,但是根據我的搜索和嘗試,失敗了。

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

$texts = $_POST['texts'];

我認為您正在尋找nl2br()函數。

nl2br()

可能的問題是新行正在顯示,但是您將其輸出為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);

也許我誤解了這個問題,但這就是我從中得到的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM