Bricktop Posted September 1, 2009 Share Posted September 1, 2009 Hi all, I'm building a simple application which writes data to a text file. Each line of this text file is treated as a separate "database entry" and the data on each line separated by ||. So for example, the text file looks like: A Name||11 The Road||The Town||The City A Name||23 The Road||The Town||The City A Name||78 The Road||The Town||The City A Name||90 The Road||The Town||The City This works great but on the data entry screen I'm using two textareas. If the users presses the enter key in the textarea and then submits, it breaks the text file up so it looks like: A Name||11 The Road||The Town||The City A Name||23 The Road||The Town||The City A Name||78 The Road||The Town||The City A Name||90 The Road||The Town||The City A Name||200 The Road|| The Town|| The City Obviously, the code which outputs the above then goes wrong and everything breaks When writing the data to the file I've tried using trim($variable) and nl2br($variable), I've also tried using trim and nl2br on the POSTed data before it's written to the text file but that doesn't work either. (If using nl2br I get the <br />'s appear in the text file but the carriage return is still present.) Any ideas? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/172691-solved-removing-newlines-from-textarea/ Share on other sites More sharing options...
ignace Posted September 1, 2009 Share Posted September 1, 2009 I don't entirely understand what you mean. Do you mean the user enters: A Name 11 The Road The Town The City In the textarea? But also could enter: A Name 11 The Road The Town The City Quote Link to comment https://forums.phpfreaks.com/topic/172691-solved-removing-newlines-from-textarea/#findComment-910262 Share on other sites More sharing options...
Bricktop Posted September 1, 2009 Author Share Posted September 1, 2009 Yes, if they enter: A Name 11 The Road The Town The City (without pressing enter in the textarea box) it works fine and the data gets written to the text file properly. If they enter: A Name 11 The Road The Town The City (obviously pressing enter at the end of every line) the data gets written to the text file but at the end of each line it starts a new line in the text file. So instead of A Name||11 The Road||The Town||The City I get: A Name|| 11 The Road|| The Town|| The City Quote Link to comment https://forums.phpfreaks.com/topic/172691-solved-removing-newlines-from-textarea/#findComment-910267 Share on other sites More sharing options...
Bricktop Posted September 1, 2009 Author Share Posted September 1, 2009 Sorted it with a simple str_replace: $text = str_replace("\r\n",'<br />',$text); Thanks anyway! Quote Link to comment https://forums.phpfreaks.com/topic/172691-solved-removing-newlines-from-textarea/#findComment-910284 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.