Any alternatives to using <pre> when displaying db text?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mirra1515
Forum Commoner
Posts: 29
Joined: Fri Apr 25, 2008 3:17 am

Any alternatives to using <pre> when displaying db text?

Post by mirra1515 »

I have a system that allows users to add an article to a mysql database by inputting the article into a textbox and then simply inserting it into the database. The problem (as you probably already know) is that when you go to select (display) that same article from the database it is no longer formatted with line breaks. I don't want my users to have to know to input a <br /> tag every time they want a line break.

The PRE tag just leads to mayhem since it is highly unreliable. Is there any other way to make sure that the article has the same line breaks (when displayed from the database) as when the user inputed it.

I am open to any method required to get this done. Thanks in advance!
User avatar
Kastor
Forum Newbie
Posts: 24
Joined: Thu May 01, 2008 2:29 am
Location: Grodno, Belarus
Contact:

Re: Any alternatives to using <pre> when displaying db text?

Post by Kastor »

Look on function nl2br() (Inserts HTML line breaks before all newlines in a string)
Or use something like this str_replace("\n", "<br>&nbsp;&nbsp;", $str);
User avatar
mirra1515
Forum Commoner
Posts: 29
Joined: Fri Apr 25, 2008 3:17 am

Re: Any alternatives to using <pre> when displaying db text?

Post by mirra1515 »

Thank you! This is perfect, and so simple...wow, never would have guessed...
Post Reply