Hello,

I have a made a forum like page where a user can submit a thread and the information gets stored in a database. However, when I try to output the data it outputs it without any line breaks in it, example:
The user inputs:
Hello,

This a paragraph

It would output like this:
Hello, This is a paragraph

I can confirm that in my database the line breaks are saved so I believe the problem is only in the displaying. This is my code used to output the information:

{Top of page}

<?php

    session_start();

    //Connected to database here

    $username = $_SESSION['username'];

    $query = "select * from threads3 where tableID='{$_GET['threadID']}'";

    $result = mysql_query($query, $conn) or die(mysql_error());

    $getinfo = mysql_fetch_row($result);

    $title = $getinfo[6];
    $content = $getinfo[8];
    $creator = $getinfo[2];

    mysql_close($conn);

?>

{output code}
        <div align='center' id='threadContentZ'>
            <p id='threadContent'><?php echo $content; ?></p>
        </div>

Recommended Answers

All 7 Replies

Member Avatar for LastMitch

@Yorkiebar14

You need to put \n at the end.

preg_replace( "/\r|\n/", "", $yourString );

I wasn't to sure what you wanted me to do but I tried using the line you gave (with the variable changed to mine obviously) but it gave me nothing at all.

Member Avatar for LastMitch

Try this:

function nl2br2($string) 
{ 
    $string = str_replace(array("\r\n", "\r", "\n"), '<br />', $string); 
    return $string; 
}  
Member Avatar for diafol

Try:

<p id='threadContent'><?php echo nl2br($content); ?></p>
commented: Nice ! +4

Thanks guys, diafols code got it working :D

Just to add some clarification here, there was never a problem with your database. It's simply that HTML does not preserve line breaks, so you need to use the nl2br() function to turn new line breaks into HTML line breaks.

Yeh mate, I stated that I could see the line breaks in my database so I thought the problem was in the output. Thanks anyway for clarifying it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.