php 用实际的换行符代码替换 \n
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3264270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
replace \n with actual new line character code
提问by Max
I'm pulling content from a DB that has been sanitized using mysql_real_escape_string. Accordingly the new line characters now appear as "\n". The issue is that this content is displayed to users inside a < pre > tag so I cannot replace \n with < br/> for instance.
我正在从使用 mysql_real_escape_string 清理过的数据库中提取内容。因此,新行字符现在显示为“\n”。问题是此内容在 < pre > 标签内显示给用户,因此我无法将 \n 替换为 < br/> 例如。
I suppose I could replace \n with the actual utf8 character code before inserting the result inside the < pre>.
我想我可以在将结果插入 <pre> 之前用实际的 utf8 字符代码替换 \n。
Can somoneone assist here? Not using mysql_real_escape_string isn't really an option due to security policy.
有人可以在这里帮忙吗?由于安全策略,不使用 mysql_real_escape_string 并不是一个真正的选择。
Thanks.
谢谢。
回答by Mark Baker
echo '<pre>'.str_replace('\n', "\n", $string).'</pre>';
回答by R. Hill
str_replace("\n","\n",$data);
回答by Lou
why not ...
为什么不 ...
echo str_replace('\n', PHP_EOL, "few lines\nof text here,\nblah, blah! etc!");
回答by symcbean
I'm pulling content from a DB that has been sanitized using mysql_real_escape_string. Accordingly the new line characters now appear as "\n"
我正在从使用 mysql_real_escape_string 清理过的数据库中提取内容。因此,新行字符现在显示为“\n”
If you've not done anything to the raw data pulled back from the database then the ptoblem is that it has been 'sanitized' twice when inserted.
如果你没有对从数据库中拉回的原始数据做任何事情,那么问题是它在插入时已经“清理”了两次。
C.
C。
回答by Nico
This might help a bit:
这可能会有所帮助:
echo('test\ntest'); shows as
echo('测试\n测试'); 显示为
test test
测试一下
but echo("test\ntest"); shows as
但是 echo("test\ntest"); 显示为
test
测试
test
测试

