[web] Add newline's

Started by
3 comments, last by intransigent-seal 15 years, 10 months ago
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 &#106avascript/PHP or html code?
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.
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]
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.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

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
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.

This topic is closed to new replies.

Advertisement