How to use nl2br() to handle string with '\\r\\n'?

放肆的年华 提交于 2019-11-28 11:42:44

You could translate them into their respective escape sequences before passing the string through nl2br(), like this:

$description = nl2br(str_replace('\\r\\n', "\r\n", $description));

But what are the literal escapes doing in your database in the first place?

Dimitry

You are storing the literal value of \r\n in your database, not the actual characters they represent.

Verify this in your database. If you see \r\n in the description field, then you're probably escaping the backslash when you're storing the data.

mfonda

It looks like your text contains the individual characters \, r, \, and n, and does not contain actual newline characters. As such, str_replace() should get the job done:

echo str_replace('\r\n', '<br>', $description);
1tiger1

The nl2br can take a second (optional) argument for "is_xhtml" which will convert the \r\n into a <br> for you. Just change your line to:

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