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
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.