views:

58

answers:

4

Hi, so I need the user to write a review about an article or book and send it to a DB via PHP but with some basic HTML formatting.. I mean, I have a form , when the user writes the review, the data is sent but without any kind of formatting, If the user want to write in a new line, the text is sent like plain text, I need to get also those new line breaks and simple stuff.

I know how to use PHP and DB connection, I just need to know how to get those new line breakes and stuff..

A: 

Use nl2br

Just before printing on the screen data from DB. It replaces \n (new line) as <br>

falkon
but the plain text saved in DB doesn't have any \n, I think...
Jonathan
it depends on DB what you are using. In MySQL new-line's are added in plain text (but they are transparent for client)
falkon
A: 

I recommend storing the data as plaintext, and adding the formatting on the way out. This way if you want to change the way it is formatted then you don't have to update every row in the database.

you can use nl2br() if you just need to newlines to be formatted, and a search-and-replace for anything else.

Nathan Reed
A: 

Have you considered using an existing 'plain text to markup' solution, like Markdown?

It (and others like it) allow your users to write plaintext reviews that will be sensibly formatted. (like stackoverflow uses!)

adamnfish
A: 

The PHP function nl2br() basically takes every new line your user enters via the form and converts the new line code to a <br> tag.

An example of using this would be:

$text = nl2br("This is text \nThis is a new line of text");

This would create the following code in your database:

This is text<br>This is a new line of text

When the user hits enter in the form textarea, PHP will pick this up as \n.

Liam Spencer