Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Preserve Formatting after mySQL Insert from PHP form

or any other formatting really



2:46 pm on Sep 25, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


Working on an admin section to insert data in a mySQL db. What I would like to be able to do is preserve the formatting when it is inserted and when I get it to spit out the info.

I figure it has something to do with \r but I cannot seem to find much on it.

Are there any other pitfalls that I need to know of like erors that is caused by php and forms. I am doing server side validation to make sure the correct things are inserted.

Cheers and thanks in advance.


4:15 pm on Sep 25, 2002 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

What type of fields are they? Do you mean from a textarea?


5:37 pm on Sep 25, 2002 (gmt 0)

10+ Year Member

Hi ukgimp

> preserve the formatting

> something to do with \r

If you're dealing with data from a <textarea> and want to preserve carriage returns (\r), and perhaps tabs (\t) used as indents (although tabbing in a text area takes you to the next form element they may be 'cut-and-pasted' to the form), use the nl2br() [php.net] function to change newlines into line breaks and str_replace() [php.net] or ereg_replace() [php.net] to search the string(s) for tabs and replace them with 3 or 4 no-break-spaces.

If you're hard-coding the formatting, and you're running php.exe on a Windows machine, where you would use \n on a *nix machine, use \r\n.

Preserving formatted blocks of code or text can be done with "here document" syntax; any formatting between <<<EOF and EOF will be preserved. The block can be stored in a variable:

$cBlock = <<<EOF

code block


... the semi-colon is for ending the variable assignment - not for ending the EOF.
NB - I don't know much more about "here docs" than this.

Before sending your data to the database you might use serialize() [php.net] to maintain data integrity, and then unserialize() [php.net] after retrieving it.


10:29 am on Sep 27, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Thanks for that Toadhall


nl2br($variable) is very easy indeed and works well.

Much better than some of the string replace functions I have encounterd over the last few days.



Featured Threads

Hot Threads This Week

Hot Threads This Month