oni-kun Posted December 17, 2009 Share Posted December 17, 2009 I'm creating a simple encoding program, and what I want it to do is retain the newlines, as echoing a textarea for example will strip them out (I think). How do I retain the \n's or whatnot and place them into a single string along with the text? will it be too much trouble? I just want it simple so I can nl2br() it in the end. I don't know what html/PHP does when it POSTS's data from a textarea. Quote Link to comment https://forums.phpfreaks.com/topic/185463-keep-newlines-from-textarea/ Share on other sites More sharing options...
rajivgonsalves Posted December 17, 2009 Share Posted December 17, 2009 I don't think they will get stripped out if your textarea content is in a variable. Quote Link to comment https://forums.phpfreaks.com/topic/185463-keep-newlines-from-textarea/#findComment-979158 Share on other sites More sharing options...
oni-kun Posted December 17, 2009 Author Share Posted December 17, 2009 I don't think they will get stripped out if your textarea content is in a variable. So if I retrieve $_POST['textareacontents'] , It will retain \n's? I'm not sure how to do this.. I want it like "string\nline2\nbla bla \nbla", and when I unencode the string, I can apply nl2br, but I don't know if the \n's even exist or whatever Quote Link to comment https://forums.phpfreaks.com/topic/185463-keep-newlines-from-textarea/#findComment-979160 Share on other sites More sharing options...
premiso Posted December 17, 2009 Share Posted December 17, 2009 You can easily try this out: <?php if (isset($_POST['submit'])) { echo "Test without nl2br(): " . $_POST['test'] . "<br />"; echo "Test with nl2br(): " . nl2br($_POST['test']) . "<br />"; } ?> <form method="post" action=""> <textarea name="test"></textarea> <br /><input type="submit" name="submit" value="submit!" /> </form> And see how it works. If the second text contains <br />'s then yes they contain \n characters. Alternatively, you can do a strstr for "\n" to check if one is contained in the string. Or if you wanted to visually see them do a str_replace and replace \n with /n and it will show you where they are. Quote Link to comment https://forums.phpfreaks.com/topic/185463-keep-newlines-from-textarea/#findComment-979161 Share on other sites More sharing options...
Adam Posted December 17, 2009 Share Posted December 17, 2009 You won't be able to see them as literally "\n" within the browser (or by viewing the source) because the browser interprets them the same as if you'd hit return a few times in a text editor. Quote Link to comment https://forums.phpfreaks.com/topic/185463-keep-newlines-from-textarea/#findComment-979163 Share on other sites More sharing options...
oni-kun Posted December 17, 2009 Author Share Posted December 17, 2009 I try this: <html> <?php if (isset($_POST['submit'])) { $test = base64_encode($_POST['test']); $test = base64_decode($test); echo "Test without nl2br(): " . $test . "<br />"; echo "Test with nl2br(): " . nl2br($test) . "<br />"; } ?> <form method="post" action=""> <textarea name="test"></textarea> <br /><input type="submit" name="submit" value="submit!" /> </form> </html> And it works and retains the newline, yeah, I was suspecting they were invisible or something of the sort. Glad it can be transported. Quote Link to comment https://forums.phpfreaks.com/topic/185463-keep-newlines-from-textarea/#findComment-979165 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.