Search code examples
phpmysqlhtmlecholines

Echo multiple lines (from MySQL database) inside an HTML page


I'm currently using this code to print a multiple lines of text from database

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

And using this HTML code:

<p> 
<?php print $content; ?>
<p/>

The text inside the database is:

abc
def
ghi

But I'm getting this

abc def ghi

Any ideas?

Thanks.


Solution

  • echo str_replace(array("\r\n", "\n", "\r"), "<br>", $content);
    

    Problem consists in, that in text you have a line breaks "\n", "\r" or his combinations, which in html displayed as spaces (space character). To insert real "line break" in html, tag <br/> must be used. So i wrote simple example of replacing all line breaks symbols to <br> html-tag.

    In php exists special function string nl2br ( string $string [, bool $is_xhtml = true ] ) which do almost the same, but, i think, more quickly and correctly.