nl2br()在显示SQL结果时不起作用(nl2br() not working when displaying SQL results)
在我的Joomla模块上,我们使用以下代码从数据库中获取呼叫
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中显示它
print stripslashes($shouts[$i]->msg);
但是,当有人想要输入如下内容时,这会导致问题:
test line 1 test line 2
如果他们进入新行,帖子在提交后会显示如下:
test line 1rntest line 2
所以我做了一些研究并意识到我必须使用
nl2br()
,如下所示:print stripslashes(nl2br($shouts[$i]->msg));
但是,似乎没有解决这个问题。 我也尝试在帮助器中创建另一个函数来使用
preg_replace
替换它,但这也无济于事。任何人都可以解释为什么在添加
nl2br()
之后nl2br()
行nl2br()
以及如何修复它?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; }
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
So I did some research and realised I had to use
nl2br()
which I did as shown below:print stripslashes(nl2br($shouts[$i]->msg));
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.Can anyone explain why line breaking isn't working after adding
nl2br()
and how to fix it?
原文:https://stackoverflow.com/questions/13410105