将\ n转换为<br/>

时间:2011-10-25 11:28:00

标签: php

  

可能重复:
  Replacing a newline with a <br> - PHP

file_put_contents('./files/'.$count.'.txt',$input);

如何在文件的每一行附加<br/>标记,因为\ n在HTML中无法识别。 例如,文件的内容是:

A
B
C
D

html中的输出只是

ABCD

或者应该有更好的方法,例如将\n替换为<br/>

3 个答案:

答案 0 :(得分:8)

只需使用nl2br()将所有换行符转换为<br />

答案 1 :(得分:1)

在保存内容之前,只需使用nl2br()即可。它会将所有"\n"替换为<br />

file_put_contents('./files/'.$count.'.txt',nl2br($input));

答案 2 :(得分:0)

正如其他人所建议的那样,如果您只需要添加<br />标记,只需将字符串传递给nl2br()即可。

如果你想在每一行的末尾添加另一个字符串,或者在添加<br />时保留换行符,你可以这样做:

$appendStr = "arbitrary string you wish to append";
$rows = explode("\n", $str);
$contents = implode ( $appendStr . "\n", $rows );