在HTML页面内回显多行(来自MySQL数据库)

Ali*_*mad 1 html php mysql lines echo

我目前正在使用此代码从数据库中打印多行文本

$query_content= "select * from home ";
$result_content= mysql_query($query_content,$con);
while ($text = mysql_fetch_array($result_content))
{
    $content = $text['homecontent']; 
}
Run Code Online (Sandbox Code Playgroud)

并使用此HTML代码:

<p> 
<?php print $content; ?>
<p/>
Run Code Online (Sandbox Code Playgroud)

数据库中的文本是:

abc
def
ghi
Run Code Online (Sandbox Code Playgroud)

但我得到了这个

abc def ghi
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

谢谢.

Cle*_*ric 6

你可以在php中使用内置函数nl2br.将\n\r(新行)转换为html <br>.

<p> 
    <?php
        print nl2br( $content );
    ?>
<p/>
Run Code Online (Sandbox Code Playgroud)

如果,并希望你有一个xhtml或html5兼容的网站,你应该把第二个参数,true以使<br>xhtml兼容,<br />

<p> 
    <?php
        print nl2br( $content, true );
    ?>
<p/>
Run Code Online (Sandbox Code Playgroud)