2

I have created a simple wall post feature, like on FaceBook.

A user writes a post, the post is submitted to the database and then echoed back onto the website; that all works.

The only issue is, when the text is echoed back onto the website the line break isn't. So, I typed:

"Hey this is a post.

Here is a new paragraph"

and it displayed as:

"Hey this is a post. Here is a new paragraph"

I've seen a few posts here saying to use the nl2br() function and input /n for a new line, however I really don't want my users to have to write '/n' everytime they want a new line, they should just have to press the enter key on the keyboard.

The line break is stored in the database, so I have no idea why it isn't echoed out. Can anyone help?

Not sure if the code will be necessary but I'll post it just in case.

while($wallposts = mysql_fetch_assoc($getwallposts)) {
    $postid = $wallposts['id'];
    $postedby_username = $wallposts['postedby'];
    $wallpostdate = $wallposts['dateposted'];
    $wallpost = $wallposts['post'];

    $querypostedby_info = mysql_query("SELECT * FROM `users` WHERE `username`='$postedby_username'");

    //get the info above
    if (mysql_num_rows($querypostedby_info)===1) {
        $getpostedby_info = mysql_fetch_assoc($querypostedby_info);

        $postedby_id = $getpostedby_info['id'];
        $postedby_profilepicture = $getpostedby_info['profilepicture'];
    }

    //display the posts
    $wallpoststicker = 
    "
    <div id='wallpost-container'>
        <div id='wallpost-header'>
            <img src='$postedby_profilepicture'><div id='wallpost-header-by'><a href='/profile.php?id=$postedby_id'>$postedby_username</a> said:</div>
            <div id='wallpost-date'>&bull; $wallpostdate</div> 
        </div>
        <div id='wallpost-content'>
            $wallpost
        </div>
    </div>
    ";
}
1
  • Try enclosing them in <pre> tags, like this: ...<div id='wallpost-content'><pre>{$wallpost}</pre></div>... Commented Feb 14, 2016 at 10:02

2 Answers 2

5

PHP function nl2br converts newlines to "<br>" breaks.
So, $wallpost=nl2br($wallpost); should accomplish the task.

Sign up to request clarification or add additional context in comments.

23 Comments

This is what echoes when I put that: "Hey this is a post. Here is a new paragraph=nl2br(Hey this is a post. Here is a new paragraph);"
@Harry Forgive my failure to understand, so are you getting the desired newline-to-BR conversion (nl2br) or not?
@Harry I stand corrected. Please enclose the $wallpost=nl2br($wallpost); line in <?php ... ?> tags and "echo" out where necessary. Thank you.
So you want me to put "<?php $wallpost=nl2br($wallpost); ?>" inside the <div id='wallpost-content'>? I'll see if that works in a second, my server is down at the moment. :/
@Harry Don't use mysql_query, it's deprecated. Use mysqli_* functions and prepared statements. For the actual query, it will return up to 3 posts if 3 are present for the condition specified in your where clause. Tom's right though, should be a new question.
|
1

The answer(s) suggesting nl2br are great, I would go with that approach.

In your original question I think there is some confusion -- users don't type \n every time they want a new line. The slash is for escaping (special character, not a normal n).

On Windows machines, the Enter key is equivalent to \r\n where \r is carriage return and \n is new line. On Linux or similar, the Enter key is \n. \r's ascii value is 13 and \n's is 10.

So an EOL (End of Line) will either be \r\n or \n. Php has the nl2br function whose purpose is to replace all \r\n or \n with <br>, the html tag for a line break.

Comments