Jump to content
  • Advertisement
Sign in to follow this  
zee_ola05

[web] [PHP/HTML] help in processing textarea

This topic is 2848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys.

I would like to print what I input in a <textarea>. My problem is, it doesn't print newlines.

e.g. I input:
"print
the
newlines
please"

It outputs: "print the newlines please"

I stored the input in $message and tried
$message = str_replace('\n', '<br />', $message) //doesn't work
echo $message

Anyone, help me in this please. Thanks!:)

Share this post


Link to post
Share on other sites
Advertisement
Hi there!

There is a function called nl2br() which takes new lines and makes them html line breaks.
Take a look here.



$message = nl2br($message);
echo $message;






Hope it helps

Share this post


Link to post
Share on other sites
Don't forget to encode the string to HTML before outputting it, else your site may be prone to XSS attacks. htmlspecialchars($message) should do the trick (though you'll need to do that before calling nl2br).

Share this post


Link to post
Share on other sites
nl2br() will work. The problem with your original code is that you are using single quotes around the \n in str_replace(). \n only matches a newline in a double quoted string, e.g "\n". In single quotes, it matches a literal backslash + letter n.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!