Replacing a new line with its html equivalent in PHP

房东的猫 提交于 2019-12-12 04:23:36

问题


I am facing a bit of a quandary, I need to replace a new line with <br />. Now, clearly, replacing all instances of \n did not work, as the page did not have proper linebreaks. Here is an example of some possible text:

Some text


More text

Now, this is an issue because there is no \n and I have no way to auto-insert <br />. How can I ensure that this contains proper linebreaks?

This is in PHP. I cannot serve it as plain text.


回答1:


To replace new line breaks with
just use nl2br




回答2:


Maybe you want http://php.net/nl2br? Or maybe I have misunderstood...




回答3:


You will want to use nl2br.

Your text has to have a newline (\n) or carriage return (\r) if the text is on 2 different lines.

nl2br will handle either case.




回答4:


php has a built in function for that.

nl2br() i believe.



来源:https://stackoverflow.com/questions/1785961/replacing-a-new-line-with-its-html-equivalent-in-php

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