Keegan Krantz
Keegan Krantz

Reputation: 21

How do you get line breaks without entering code?

I am trying to get text to enter into a database, but the text needs to have the line breaks that are entered into the textbox for when it is entered into the database, without having to enter <br /> every time there is a line break.

Upvotes: 2

Views: 168

Answers (7)

Jazi
Jazi

Reputation: 6712

nl2br() function.

Upvotes: 1

Gregg King
Gregg King

Reputation: 31

The <br /> is an HTML breakline, but in just plain text using "\n" (without the quotes) in your text will give you the newline character. If you are taking the text directly from the GET/POST (e.g. $_REQUEST["myTextField"]) and put that into the database then it should keep the line breaks as they were entered.

If the database does NOT contain the line breaks, is it possible that you are doing some input cleansing (which you should be doing) that is removing the line breaks?

Upvotes: 1

JanLikar
JanLikar

Reputation: 1306

Use a nl2br() function. Example usage: $string=nl2br($string).

Upvotes: 0

one.beat.consumer
one.beat.consumer

Reputation: 9504

If you are trying to send string data containing HTML code, you would want to use \r\n in the string to represent line breaks in the encoded string...

<sometag>\r\n\<childtag />\r\n</sometag> 

renders this:

<sometag>
    <childtag />
<sometag>

<br/> represents a linebreak only in rendered HTML not in the way HTML looks in your editor.

That help?

Upvotes: 1

Jim
Jim

Reputation: 73936

You're doing it in the wrong place. The form data submitted that you are saving to the database correctly preserves the line breaks. That's a perfectly reasonable canonical representation of the data. What you want is to format it as HTML when you are outputting the data, and for that you can use nl2br().

Upvotes: 2

simonbs
simonbs

Reputation: 8042

So you want to make line breaks in a textbox and have these turn into <br /> when posted to the database, right?

nl2br() should do it. Just use this function on your string before posting it to the database.

Upvotes: 3

macjohn
macjohn

Reputation: 1803

Save your text with \n and then in php there is a function that do the conversion

nl2br();

Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).

Upvotes: 2

Related Questions