I have a guestbook I made with my rusty knowledge of PHP and I am saving all the comments into a text file onto one line and the idea is that when I print the comments on the page I will read each line individually and print it in a <p> tag. This method will work fine for what I want to do, only two problems. (1) I need to format all the text from the textarea onto one line to go into the text file to avoid problems if the user ads a line break in the text area (2) I need to somehow add a line break to the text file when It saves the info so the reading script won't put all the text on the same line from say two different guestbook entries Help appreciated
when user add a line break in textarea, it is a "\n" character. you can convert "\n" character to "<br>" using following code $text = "Your input text from textarea\n include new line break\n for testing."; $text = preg_replace('@\n@', '<br>', $text); PHP: Now, your $text is a one line text string, you can save it to text file, or print it out ^__^
When saving the guest book entry do this: function clean($str) { $newStr =strip_tags($str); $newStr =str_replace("\r", "" , $newStr); $newStr =str_replace("\n", "" , $newStr); return($newStr); } PHP: That function will clean out any new lines or HTMl tags from the input string. When you save it you can append a new line (i.e. "\n") at the end of each guestbook entry, so that they don't all come up as one long line.
Thank you very much tnd8, that worked perfectly. Just curious, why do you put the @ in front and behind \n? But one more thing, how do I create a line break in the text file for the next users comment? I have tried this: if(isset($_REQUEST['comment'])) { $message = $_REQUEST['comment']; $message = preg_replace('@\n@', '<br>', $message); $file = fopen("GB\guestbook.txt","a"); fwrite($file,$message . "\n"); /* Theoretically this would add a line break to the text document, but it doesn't work for me? */ fclose($file); } Code (markup): @samyak Thanks that works too. See above about breaking line in text document.