> 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
... 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.