nl2br()在显示SQL结果时不起作用 [英] nl2br() not working when displaying SQL results
问题描述
在我的Joomla模块上,我们使用以下代码从数据库中获取喊叫
On my Joomla module, we are using the following code to get shouts from the database
function getShouts($number, $timezone, $message) {
$shouts = array();
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__shoutbox')
->order('id DESC');
$db->setQuery($query , 0 , $number);
$rows = $db->loadObjectList();
$i=0;
foreach ( $rows as $row ) {
$shouts[$i]->id = $row->id;
$shouts[$i]->name = $row->name;
$shouts[$i]->msg = $row->msg;
$i++;
}
return $shouts;
}
和以下代码将其显示在 default.php
and the following code to display it in the default.php
print stripslashes($shouts[$i]->msg);
但是,当有人要输入以下内容时,这会引起问题:
However this is causing problems when someone wants to input something like the following:
test line 1
test line 2
如果他们改行,则帖子在提交后显示如下:
If they go onto a new line, the post displays like so after being submitted:
test line 1rntest line 2
所以我做了一些研究,意识到我必须使用nl2br()
,如下所示:
So I did some research and realised I had to use nl2br()
which I did as shown below:
print stripslashes(nl2br($shouts[$i]->msg));
但是,它似乎无法解决问题.我还尝试在辅助程序中创建另一个函数,以使用preg_replace
替换它,但这也无济于事.
however, it didn't seem to resolve the issue. I also tried creating another function in the helper to replace it using preg_replace
but this didn't help either.
谁能解释为什么添加nl2br()
后换行不起作用以及如何解决?
Can anyone explain why line breaking isn't working after adding nl2br()
and how to fix it?
推荐答案
尝试这种方式:
print nl2br(stripcslashes($shouts[$i]->msg));
请注意stripcslashes()
功能,而不要注意stripslashes()
Pay attention to the stripcslashes()
function, but not stripslashes()
或者只是:
print nl2br($shouts[$i]->msg);
UPD::nl2br()
函数将\n
替换为<br />
.问题是您的文本中没有\n
,但是有n
或\\n
.我认为在从库中获取数据时无需使用stripslashes()
,除非您在库中已对数据进行了遗留化.
UPD: nl2br()
function replaces \n
with <br />
. The problem is that you don't have \n
in your text, but have n
or \\n
. I think there is no need to use stripslashes()
when you get data from base, except the situation when you have ecranized data in your base.
这篇关于nl2br()在显示SQL结果时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!