eldorik Posted September 15, 2007 Share Posted September 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted September 15, 2007 Share Posted September 15, 2007 Try the wordwrap() function. http://php.net/wordwrap Quote Link to comment Share on other sites More sharing options...
eldorik Posted September 15, 2007 Author Share Posted September 15, 2007 That just makes it so when the string is a length it makes it a new line.' I want to be able to set where it is a new line in the text field by just pressing enter. Quote Link to comment Share on other sites More sharing options...
AndyB Posted September 15, 2007 Share Posted September 15, 2007 nl2br() on text drawn from the database Quote Link to comment Share on other sites More sharing options...
eldorik Posted September 15, 2007 Author Share Posted September 15, 2007 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: And heres the page that views the result: Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted September 15, 2007 Share Posted September 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 15, 2007 Share Posted September 15, 2007 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: And heres the page that views the result: 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? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 15, 2007 Share Posted September 15, 2007 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 />). Quote Link to comment Share on other sites More sharing options...
adrianTNT Posted September 15, 2007 Share Posted September 15, 2007 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.