Jump to content

Keep newlines from textarea?..


oni-kun

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/185463-keep-newlines-from-textarea/
Share on other sites

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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.