[英]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.