replace \n with actual new line character code

允我心安 提交于 2020-01-01 08:02:12

问题


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.

I suppose I could replace \n with the actual utf8 character code before inserting the result inside the < pre>.

Can somoneone assist here? Not using mysql_real_escape_string isn't really an option due to security policy.

Thanks.


回答1:


echo '<pre>'.str_replace('\n', "\n", $string).'</pre>';



回答2:


str_replace("\\n","\n",$data);



回答3:


why not ...

echo str_replace('\\n', PHP_EOL, "few lines\nof text here,\nblah, blah! etc!");



回答4:


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"

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.




回答5:


This might help a bit:

echo('test\ntest'); shows as

test test

but echo("test\ntest"); shows as

test

test



来源:https://stackoverflow.com/questions/3264270/replace-n-with-actual-new-line-character-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!