0

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>
4
Contributors
7
Replies
9
Views
5 Years
Discussion Span
Last Post by Yorkiebar14
2

@Yorkiebar14

You need to put \n at the end.

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

0

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.

2

Try this:

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

Try:

<p id='threadContent'><?php echo nl2br($content); ?></p>
Votes + Comments
Nice !
0

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.

0

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 :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.