Jump to content
  • Advertisement
Sign in to follow this  
MadMax1992

[web] Add newline's

This topic is 3842 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

Goodevening gamedev! I'm here with a small problem, I just learned a little bit of PHP, mySQL and html, so I started making a site (on a home-server, so it can't be tested) where you can log in, create and post polls, and vote on them. The problem is that when I try to add text for the poll (through an online input field), the text becomes one large piece of text (without newline's, tabs etc.). Can I fix this with javascript/PHP or html code?

Share this post


Link to post
Share on other sites
Advertisement
If I understand what you're saying, then you may wish to do two things.

First, look into the PHP function htmlspecialchars. Call this on your submitted string and capture the returned string. This will convert the submitted text to the appropriate HTML encodings for display

Second, a quick fix for the whitespace formatting is to surround the outputted text with < pre >< /pre > tags (without the spaces).

Alternative to the pre tags, you would scan through the string and replace new lines with < br /> tags, tabs with either a few '& nbsp;' statements (again, without the space) or an inline div with a width to force an indention, and so on.

Share this post


Link to post
Share on other sites
I am so stupid, I figured it out in a couple of hours:

Note to self: Use google

If anybody comes across this topic, this is the solution:

ereg_replace is a PHP function that replaces a certain character from a string with another character. So you can use it like this:


ereg_replace("\n", "<br />", $string);
{/source]

Share this post


Link to post
Share on other sites
PHP has a built-in function for this, nl2br.

ereg_replace (and preg_replace) are designed to work with regular expressions, so are overkill for replacing simple substrings. Use str_replace if you just wish to just replace one string with another.

Share this post


Link to post
Share on other sites
Quote:
Original post by MadMax1992
I am so stupid, I figured it out in a couple of hours:

Note to self: Use google

If anybody comes across this topic, this is the solution:

ereg_replace is a PHP function that replaces a certain character from a string with another character. So you can use it like this:

*** Source Snippet Removed ***

Or you can just use the built in nl2br() function.

PHP has a function for most things you're likely to want to do - you just have to look at the list.

Edit: Beaten by benryves :(

John B

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!