![](/img/trans.png)
[英]Export to Excel: export text area contents in single cell “with line break within the cell /Alt + Enter” using javascript
[英]Line break issue when displaying file contents in html text area - javascript PHP
我正在嘗試將文本文件的內容顯示到文本區域元素中,但是換行符出現問題,顯示為“ \\ n”,而不是實際執行。
<textarea id = "textArea" name = "textArea" data-ng-model="note.text"></textarea>
JavaScript將文本添加到文本區域元素
var $promise=$http.post("http://localhost/PHP/findNotes.php", data); //send data to user.php
$promise.then(function(msg){
document.getElementById("textArea").value = msg.data;
});
PHP文件獲取文件內容
$note=json_decode(file_get_contents('php://input')); //get note
$noteId = ($note->noteId);
$userId = ($note->userId);
$filename = 'file://localhost/Library/WebServer/Documents/Notes/'.$userId.'/'.$noteId.'.txt';
if (file_exists($filename))
{
$file = fopen($filename, "r");
while (!feof($file))
{
$display = fgets($file, filesize($filename));
echo $display;
}
fclose($file);
}
else
{
echo "Error occurred!";
}
讀取內容文件時
line1\nline2\n\nline4
完全相同的內容顯示在文本區域中
但是,如果將文件內容直接應用於js代碼中的textarea,則換行符可以正常工作。
因此,如果JS更改為
document.getElementById("textArea").value ="line1\nline2\n\nline4";
那么文本區域中的輸出是正確的:
line1
line2
line4
希望獲得幫助,似乎是從PHP接收的文本格式有問題嗎? 提前致謝
您需要將\\ n字符轉義為ajax到\\\\ n
因此,在findNotes.php中回顯$ display之前,應插入:
$display=str_replace("\n", "\\n", $display);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.