如何使用nl2br()处理带有'\ r \ n'的字符串? [英] How to use nl2br() to handle string with '\r\n'?

查看:140
本文介绍了如何使用nl2br()处理带有'\ r \ n'的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在提交表单后从管理员通过textarea检索存储在数据库中的产品描述值.当我从数据库中选择描述时,我得到$description = $row['description'];,并且我想在主页上回显$ description,如下所示:echo nl2br($description);,但是我看到的是"\r\n"字符,而不是创建新行.根据我在网上发现的信息,必须在双引号之间使用您的字符串,如下所示:

I am retrieving a product description value stored in database from admin through textarea upon form submit. When I select the description from database I get $description = $row['description']; and I would like to echo $description on main page like this: echo nl2br($description); but I see "\r\n" characters instead of making new rows. From what I've found here and on the net, your string must be used between double quotes, like this:

echo nl2br("Hello, \r\n This is the description");

现在,数据库中$description的值实际上是"Hello, \r\n This is the description",但是在我的脚本中,我必须像这样使用它:

Now, the value of $description from database is in fact "Hello, \r\n This is the description" but in my script I have to use it like this:

echo nl2br($description);

不是br的,而是输出\r\n.因此,根据我的经验,我该怎么办,不能在此处使用双引号.

Which does not make br's, it is outputing \r\n instead. So, what can I do, I can't use double quotes here, from my experience.

推荐答案

在将字符串通过nl2br()传递之前,您可以将它们转换为各自的转义序列,如下所示:

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?

这篇关于如何使用nl2br()处理带有'\ r \ n'的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆