当打印为<;pre>;在HTML中


How to replace ` ` with new line when print as <pre> in HTML?

我从PHP服务器得到的文本文件看起来像:

"文件版本:5''n''nstart:1410355285955(2014/09/10 11:58:05.955)''nend:141090402750(2014/09/20 12:00:02.750)''UEID:301660031''nCNC:1118130''n供应商:75306''n型号:807182328''nOS:1549568403''n平台:1''n载波:-1337093915''n报告的网络:0''n''n域名数:1''n域276975141''n标签数:1''n-2032188703''n主机应用程序数:2''n主机应用程序:-2097733624,主机版本300009300500000。。。

如何用新线路替换'n

预期结果应为:

File version: 5
start:    1410450285955(2014/09/10 11:58:05.955)
end:      1413350402750(2014/09/10 12:00:02.750)
UEID:     301600031
CNC:      114130
Vendor:   75506
Model:    827112328
OS:       1539568403
Platform: 1
Carrier:  -1347083915
Reported Networks:  0
Num domains: 1
  Domain 276955141
  Num tags: 1
  -2032160703
  Num hostedApps: 2
  HostedApp: -2052733624,  HostedVersion 3000003070500000, ...

我将其打印为:

 <pre>{{myText}}</pre>

如果是json,我会使用json过滤器:

 <pre>{{myText | json}}</pre>

这就是我在PHP中打开文件的方式:

 $file = file_get_contents($filePath, FILE_USE_INCLUDE_PATH);

如果它在pre中,则不需要替换'n,因为是换行符

document.querySelector("pre").textContent = "File version: 5'n'nstart: 1410355285955(2014/09/10 11:58:05.955)'nend: 141090402750(2014/09/10 12:00:02.750)'nUEID: 301660031'nCNC: 118130'nVendor: 75306'nModel: 807182328'nOS: 1549568403'nPlatform: 1'nCarrier: -1337093915'nReported Networks: 0'n'nNum domains: 1'n Domain 276975141'n Num tags: 1'n -2032188703'n Num hostedApps: 2'n HostedApp: -2097733624, HostedVersion 300009300500000, ..."
<pre></pre>

换行符'n被直接包含在文本中,这很奇怪。它可能在某个地方被双重逃脱。

类似于这个问题

在这种情况下,您只需要将单引号''n'中的换行符替换为双引号"'n"中的换行字符,就可以在PHP中正确解释它。尝试使用PHP的str_replace()