realjumper Posted May 15, 2007 Share Posted May 15, 2007 Hi, I know this has been asked before, but I still have a question! I'm away from work at the moment so I can't check this out. Here is the scenario: In my php page I have a textarea for people to type into. When the users type their text and submit the form, all formatting is lost. Now, I did tell the users that this isn't a word processor! Anyway, I could use buttons similar to this forum to insert html tags like <p> etc, but, believe me, the users would never get the hang of how they work and they would delete them, and then cry about no formatting! All I really need is to have line breaks, just like the one above, which I got by hitting the Enter key twice. So, when I echo the text it will at least have 2 like breaks which will be fine for a paragraph break. I have looked at the manual, and at nl2br() in particular, but I'm not exactly sure how this works. It seems to be saying that when a user hits the Enter key, either \r or \n is added instead of <p>, and the database reflects this if I have a look...apparently. I'm not at the DB so I can't have a look. So, could someone explain to me how to use nl2br() (if that is indeed the correct function), or else perhaps how to retain the line breaks and paragraph breaks that the use enters in the html textarea please? Also, if I'm reading other's experiences with the same problem, do MAC's and PC's both use either \r or \n? I'm a bit confused and most of our users use MAC's, but not all Many thanks, Neil Quote Link to comment Share on other sites More sharing options...
realjumper Posted May 15, 2007 Author Share Posted May 15, 2007 Well, I figured it out and I hope I can explain it a bit more clearly than the manual does.....just in case anyone wants to know. When you type into a textarea and you hit the Return Key on a MAC, or the Enter key on a Windoze box, a character \n is entered into the text string where you might, in html terms, have expected to see.... <br> When you submit the form, and enter the string into the database, you might think that you will be able to see \n in the database.......but you can't. But it *is* there. So, when you echo the field in question, all the text runs together and any line brakes are ignored...because the browser doesn't understand \n Therefore, we need to convert these special characters into a format that the browser will understand, and the way it's done is by using the php function nl2br() like this..... <?php //insert the formatted text in the table $query = "INSERT INTO table (text) VALUES ('$text')"; mysql_query($query); // Retrieve all the data from the table $result = mysql_query("SELECT * FROM table WHERE id='$id'") or die(mysql_error()); // store the record of the table into $row $row= mysql_fetch_array( $result ); ?> <!-- echo the text --> <?php echo ($row["text"]);?> <!-- Ugly unformatted text! --> <?php echo nl2br($row["text"]);?> <!--lovely formatted text with <br /> replacing \n --> So, I hope that is a clearer explanation than the manual appeared to me, and I hope that helps people who have this same problem. Cheers, Neil Quote Link to comment Share on other sites More sharing options...
modigy Posted June 28, 2007 Share Posted June 28, 2007 That's exactly what I needed!!! Thanks, Modigy 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.