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

## 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 on other sites
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 on other sites
I am so stupid, I figured it out in a couple of hours:

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 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 on other sites
Quote:
 Original post by MadMax1992I am so stupid, I figured it out in a couple of hours:Note to self: Use googleIf 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

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013653
×