Jump to content

Need help with adding <br /> or new line to a variable.


eldorik

Recommended Posts

I need to add

<br />

or a new line to the end of every line for my variable that contains the content for my news.

 

The news is stored in a MySQL database and is retrieved using mysql_fetch_assoc().

 

At the moment it just shows up as one continuous line, but I want it to have paragraphs.

 

Does anyone know any code that I can add that will make it have new lines?

 

Thanks.

Link to comment
Share on other sites

The text in the database has NOT got any /n's in it at all. It is just plain text but when viewed in phpMyAdmin it shows the lines.

 

Heres a screenshot of phpMyAdmin:

 

phpmyadmin-newstable.jpg

 

And heres the page that views the result:

 

mainpage-newsresults.jpg

 

 

Yes, it has got them. You just can't see them, because phpMyAdmin is converting them for you.

Did you try using the function yet?

Link to comment
Share on other sites

Oops, I didn't know you were typing this text in a text field.

 

Use nl2br() BEFORE inserting it into the DB, that will automatically add the <br>'s.

www.php.net/nl2br

Dont use nl2br before when you insert the text into the database. It is best to use it after you insert it into the database.

 

PHP is not stripping the newlines. The problem is to do with the web browser. Browsers ignore whitespace characters. This is why your text is shown on one line. However if you view the source you'll notice the text is formatted the way you had typed into to the textarea. Using nl2br it will convert newlines into an html line break (<br />).

Link to comment
Share on other sites

I just had a similar issue, I was printing listing descriptions taken form the database and in the database it looked like plain file but on multiple lines (no <br /> in it there).

Printing it on screen made the words come one after another without new lines, I used nl2br() and now it looks perfect, it shows the text on new lines.

Also... when editing the listing I load the text in a text area directly from database and it looks ok in the text area.

So you only need the nl2br()  when printing the text on screen. :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.