在html文本区域显示文件内容时出现换行问题-javascript PHP

在html文本区域显示文件内容时出现换行问题-javascript PHP,javascript,php,textarea,line-breaks,Javascript,Php,Textarea,Line Breaks,我正在尝试将文本文件的内容显示到文本区域元素中,但遇到了将换行符显示为“\n”而不是实际执行此操作的问题 <textarea id = "textArea" name = "textArea" data-ng-model="note.text"></textarea> PHP文件以获取文件内容 $note=json_decode(file_get_contents('php://input')); //get note $noteId = ($note->no

我正在尝试将文本文件的内容显示到文本区域元素中,但遇到了将换行符显示为“\n”而不是实际执行此操作的问题

<textarea id = "textArea" name = "textArea" data-ng-model="note.text"></textarea>
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收到的文本格式似乎有问题?
提前感谢

您需要将ajax的\n字符转义为\\n

因此,在findNotes.php中回显$display之前,应插入:

$display=str_replace("\n", "\\n", $display);

我认为正确的版本是$display=str_replace(“\\n”、“\n”、$display”);但谢谢你,这很有效。如果你修改你的答案,我会接受的。很高兴听到你的问题解决了。但是如果您想用第二个反斜杠转义,\n我的代码是正确的。从手册中:
mixed str\u replace(mixed$search,mixed$replace,mixed$subject[,int&$count])
Oh right出于某种原因,文本保持不变,除非我更改了\\n和\n
$display=str_replace("\n", "\\n", $display);